ios - 如何在单元格获得自定义高度时自定义 UITableView 分隔符

标签 ios iphone uitableview

我关注了这篇文章:How to customize tableView separator in iPhone

问题是,当我为我的单元格设置自定义高度时,它无法正常工作。

我将向您展示两张图片,一张有两条线的图片是我的单元格具有自定义高度的结果。

enter image description here

自定义高度: enter image description here

- (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/

相关文章:

ios - NSOperationQueue 批处理 SLRequests

ios - 请求位置 Swift iOS 9

iphone - 减少 iPhone 上的核心显卡内存消耗

IOS访问 block 外的变量

ios - 在 UITableViewCell 中自动布局两个标签?

ios - 将数据传递给 UITableViewCell iOS

ios - 是否可以在 iphone 上的多个应用程序之间共享守护进程?

iphone - 从预览内容生成的文档缩略图

iphone - 扩展教程 Material 时 entityForName 和 ManagedObjectContext 的问题

ios - UIActivityIndi​​catorView float 在 UITableView 之上