【问题标题】:How to customize UITableView separator when the cells got a custom height当单元格获得自定义高度时如何自定义 UITableView 分隔符
【发布时间】:2014-12-10 16:25:22
【问题描述】:

我关注了这个帖子: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;
}

【问题讨论】:

  • 自定义高度是只针对一个单元格还是所有单元格?
  • 您有自定义单元格吗?在一个笔尖?问题是在设置 lineView 后单元格会展开。如果你想让它粘在底部,你可以查看 autoresizingMask 或者设置 autoLayout 约束

标签: ios iphone uitableview


【解决方案1】:

您好,问题出在 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;

【讨论】:

  • 这看起来很有希望,我回到办公室时会测试它,谢谢!
  • Onik 的回答是对的,只有一件事 - 更好的是,如果你不硬编码布局,而是覆盖 UITableViewCell layoutSubviews 函数并在那里放置类似self.lineView.frame = CGRectMake(0, self.frame.size.height - 1, self.frame.size.width, 1);
猜你喜欢
  • 1970-01-01
  • 2011-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-17
  • 1970-01-01
  • 2012-08-01
相关资源
最近更新 更多