【问题标题】:configure cell drawRect : custom drawRect only appears after a drag in tableView?配置单元格drawRect:自定义drawRect仅在tableView中拖动后出现?
【发布时间】:2015-02-06 08:09:43
【问题描述】:

我有一个 splitViewController,destinationController 是一个 tableViewController,如果自定义类“MyCell”,我想控制单元格的设计。只有当我拖动 tableView 时才会调用 drawRect :只有在单元格从屏幕上消失并重新出现后才会出现红色背景。你知道为什么吗?

//in viewController.m
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MyCell*cell = (MyCell*)[tableView dequeueReusableCellWithIdentifier:@"CellDetail"] ;
    [self configureCell:cell atIndexPath:indexPath];
    return cell;
}
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
    RDV *object = [self.fetchedResultsController objectAtIndexPath:indexPath];
    cell.textLabel.text = [[object valueForKey:@"name"] description];
    [cell setNeedsDisplay];
    [cell layoutIfNeeded];
}



//in MyCell.m
-(void)drawRect:(CGRect)rect{
    [self setBackgroundColor: [UIColor redColor]];
}

【问题讨论】:

    标签: ios uisplitviewcontroller drawrect custom-cell


    【解决方案1】:

    我不确定为什么在滚动之前颜色变化不会出现,但无论如何你都不应该为此目的使用 drawRect。将背景颜色代码放在单元格的 init 方法或 awakeFromNib 中。

    【讨论】:

    • 谢谢rdelmar,答案很棘手,我不得不使用MyRealCell* cell = (MyRealCell*)[tableView dequeueReusableCellWithIdentifier:@"MyCell" forIndexPath:indexPath];forIndexPath,这样,contentView框架包含一些东西,否则它是0,0,0,0。跨度>
    • @Paul,这与设置颜色有什么关系?
    • 如果 frame 为 0,0,0,0,则在空内绘制,而如果 contentView 有 frame,则可以出现渐变色。
    • @Paul,这很奇怪。这两种方法返回的单元格之间应该没有任何区别。
    【解决方案2】:

    答案有点棘手,我不得不使用MyRealCell* cell = (MyRealCell*)[tableView dequeueReusableCellWithIdentifier:@"MyCell" forIndexPath:indexPath];forIndexPath,这样,contentView 框架包含一些东西,否则是 0,0,0,0。

    【讨论】:

      【解决方案3】:

      我假设您没有在表格视图中注册单元格。那里为

      MyCell*cell = (MyCell*)[tableView dequeueReusableCellWithIdentifier:@"CellDetail"] ;
      

      返回nil。因为您可以向nil 发送任何消息,并且始终会返回0x0 中的nil,请求框架会返回CGRectZeo

      为什么拖动确实会使单元格出现并不明显。你实现tableView:cellWillAppear: 或类似的吗?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多