【问题标题】:Why can't I change UITableViewCell's ContentView after initiationUITableViewCell的ContentView启动后为什么不能改
【发布时间】:2014-07-08 21:44:56
【问题描述】:

我可以在我的 uitableviewcells 上方手动添加一个“行分隔符”(开箱即用的 iOS 没有为此剪掉它)。所以我创建了一组 UIView,称为 _separatorLines,我将每个索引中的一个 UIView 添加到我的“cellForRowAtIndexPath”中的 uitableviewcells 的 contentView 中

  UIView *v         = [_separatorLines objectAtIndex:indexPath.row];
  [cell.contentView addSubview:v];

但是,当我选择一个单元格时,我希望所有其他单元格的行分隔符变为红色。 在 'didSelectRowAtIndexPath' 我有:

{
for(unsigned int i = 0; i < _rowsInSection-1; i ++)
{
  //make all other rows have a red content view
  if(i != indexPath.row)
  {
            NSIndexPath *I = [NSIndexPath indexPathForRow:i inSection:1];
            UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:I];

            UIView *separatingLineView = [_separatorLines objectAtIndex:I.row+1];
            separatingLineView.backgroundColor = [UIColor redColor];
            [cell.contentView bringSubviewToFront:separatingLineView];
            [separatingLineView setNeedsDisplay];

            [cell setNeedsDisplay];
            [cell.contentView setNeedsDisplay];
        }
    }

  [self setNeedsDisplay];

相反,我没有看到 uitableviewcells 的 contentView 发生变化。

【问题讨论】:

    标签: uitableview setcontentview


    【解决方案1】:

    对我来说,我认为问题在于线路 NSIndexPath *I = [NSIndexPath indexPathForRow:i inSection:1];

    UITableView 从“0”部分开始(就像行从“0”开始)。

    也因为我的 UITableView 是委托,我停止使用

    [self tableView:tableView cellForRowAtIndexPath:I];
    

    替换为

    [self cellForRowAtIndexPath:I];
    

    不知道有没有关系

    【讨论】:

      猜你喜欢
      • 2011-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-03
      • 2014-11-23
      • 1970-01-01
      • 1970-01-01
      • 2021-08-03
      相关资源
      最近更新 更多