【问题标题】:Separator line issues - uitableview cells with different heights分隔线问题 - 不同高度的 uitableview 单元格
【发布时间】:2015-09-10 05:41:19
【问题描述】:

我有一个具有不同单元格高度的 UITableView。我还使用以下代码添加了一个自定义分隔线

UIView* separatorLineView = [[UIView alloc]initWithFrame:CGRectMake(1, cellReport.frame.size.height-1, table.frame.size.width-1, 1)];
separatorLineView.backgroundColor = [UIColor grayColor];
[cellReport.contentView addSubview:separatorLineView];

当我滚动表格时,行之间会出现额外的分隔符,我不明白为什么?我错过了什么吗?帮帮我。 简而言之,UITableView 在某些单元格的错误位置显示分隔符。已附上示例图像以供参考。

P.S:我没有使用自动布局

【问题讨论】:

  • 试试这个框架:CGRectMake(cell.frame.origin.x,cell.frame.size.height-1,cell.frame.size.width,1)
  • @SaurabhPrajapati 实际上我只在我的代码中使用了它。它不起作用。我已经编辑了我的答案。请看一下
  • 您使用的是自定义单元格吗?然后将 cell.contentView addSubview:separatorLineView 替换为 ** cellReport.contentView addSubview:separatorLineView** 可能有问题

标签: ios objective-c iphone uitableview


【解决方案1】:

请按照以下步骤操作

并且不需要每次都查看分隔符。并且每次都设置好你的框架。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *myIdentifier=@"Cell";

    UITableViewCell  *Cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:myIdentifier];

    UIView* separatorLineView;

    if (Cell == nil)
    {

        Cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:myIdentifier];

        separatorLineView = [[UIView alloc] initWithFrame:CGRectZero];
        separatorLineView.backgroundColor = [UIColor grayColor];
        separatorLineView.tag = 100;
        [Cell.contentView addSubview:separatorLineView];
    }

    separatorLineView = (UIView *)[Cell.contentView viewWithTag:100];

    separatorLineView.frame = CGRectMake(0, Cell.frame.size.height-1, Cell.frame.size.width, 1);

}

【讨论】:

  • 谢谢。完美的解决方案。它就像一个魅力:)
猜你喜欢
  • 2014-11-19
  • 1970-01-01
  • 2015-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多