2010-01-05 23 views
3

我有一个高度为400px的UITableView,我想用10或11个自定义UITableViewCells来填充,具体取决于要显示的数据。问题是,根据我如何使用我当前的方法设置每行高度,单元格之间或底部单元格之下存在间隙。我认为这是由于四舍五入。使用动态行高时UITableView间隙

这个代码把分散的1px的差距,在某些细胞的末尾:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (isWednesdaySchedule) { 
     return (tableView.frame.size.height/11); 
    } 
    else { 
     return (tableView.frame.size.height/10); 
    } 
} 

而且这种代码,铸造到NSIntegers的回报,使所有的细胞结合在一起,但留下少数的差距底部单元下面的像素:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (isWednesdaySchedule) { 
     return (NSInteger)(tableView.frame.size.height/11); 
    } 
    else { 
     return (NSInteger)(tableView.frame.size.height/10); 
    } 
} 

我怎样才能解决这个问题,使我所有的细胞没有间隙在最后一个单元格之间或下方显示?

回答

1

那么,如果表格的边界恰好可以被10或11整除,那么您只会看到一个无间隙表格。表格的设计假定行将从屏幕滚动出来,以便尝试填满屏幕。

我认为你有两个选择:

(1)设置行的高度,使得底部行中途关闭屏幕。用户将向上滚动查看最后一行,行之间不会存在明显的间隙。最后会有死角,但用户期望在桌上。 (2)使用部分页眉和页脚视图以可视方式填充单个部分顶部和底部的空间,以便根据需要进行定位。我很肯定你可以使用任意高度的空白透明视图来按需要居中放置该部分。

0

您可以使底部单元格更大一点以填充空间。如果大小差别不大,可能不明显。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (isWednesdaySchedule) { 
     if (indexPath.row < 10) { 
      return (NSInteger)(tableView.frame.size.height/11); 
     } else { 
      CGFloat bottomPadding = tableView.frame.size.height ((NSInteger)(tableView.frame.size.height/11)*11; 
      return (NSInteger)(tableView.frame.size.height/11 + bottomPadding); 
     } 
    } else { 
     if (indexPath.row < 9) { 
      return (NSInteger)(tableView.frame.size.height/10); 
     } else { 
      CGFloat bottomPadding = tableView.frame.size.height ((NSInteger)(tableView.frame.size.height/10)*10; 
      return (NSInteger)(tableView.frame.size.height/10 + bottomPadding); 
     } 
    } 
} 

代码还没有经过测试,但我认为你明白了。

1

如果你实际上并不需要单元具有不同的高度,为什么不改变UITableView的高度取决于isWednesdaySchedule?这应该解决你提到的问题。