当单元格达到自定义高度时如何自定义UITableView分隔符 [英] How to customize UITableView separator when the cells got a custom height
问题描述
我关注了这篇文章:如何在iPhone中自定义tableView分隔符
问题是,当我为单元格设置了自定义高度时,它不能很好地工作.
Problem is that it doesn't work well when I have custom height for my cell.
我将为您展示两张图片,其中两行是为我的单元格自定义高度的结果.
I'll show you with two images, the one with two lines is the result of having a custom height for my cells.
具有自定义高度:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, cell.contentView.frame.size.height - 1.0, cell.contentView.frame.size.width, 1)];
lineView.backgroundColor = [UIColor darkGrayColor];
[cell.contentView addSubview:lineView];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 50;
}
推荐答案
问题出在cellForRowAtIndexPath方法中,您的单元格尚不知道它的高度.如果您使用自定义高度(您知道此高度,也可以在cellForRow中使用它...).
Hi the problem is in the cellForRowAtIndexPath methods you cell doesn't know its height yet. If you use a custom height (you know this height, use it in the cellForRow... also).
这里是示例:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 49.0, cell.contentView.frame.size.width, 1)];
lineView.backgroundColor = [UIColor darkGrayColor];
[cell.contentView addSubview:lineView];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 50;
}
此外,别忘了您可以使用spacerStyle和spacerInset来自定义此行的代码.如果您不使用它,则将其置为空:
Also, don´t forget you can use separatorStyle and separatorInset, in order to custom a litle this line. If you don't use it put to none:
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
这篇关于当单元格达到自定义高度时如何自定义UITableView分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!