我关注了这篇文章:How to customize tableView separator in iPhone
问题是,当我为我的单元格设置自定义高度时,它无法正常工作。
我将向您展示两张图片,一张有两条线的图片是我的单元格具有自定义高度的结果。
自定义高度:
- (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 中使用它...)。
这里的例子:
- (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;
}
此外,不要忘记您可以使用 separatorStyle 和 separatorInset,以便稍微自定义这一行。如果您不使用它,则将其置为无:
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
关于ios - 如何在单元格获得自定义高度时自定义 UITableView 分隔符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27406014/