2014-01-14 40 views
0

我有一段时间让单元的backgroundView显示。UITableViewCell backgroundView绘制矩形不被称为

代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    // assume cell is dequeued correctly (i.e. cell != nil) 

    UIView *backgroundView = [[UIView alloc] initWithFrame:cell.frame]; 
    backgroundView.backgroundColor = [UIColor yellowColor]; 
    cell.backgroundView = backgroundView; 
    [cell.backgroundView setNeedsDisplay]; 

    return cell; 
} 

我也- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath试过这个。仍然没有运气。

我试图继承用作backgroundView的UIView对象,并且确认drawRect从不被调用。

回答

1

你可能不应该在UITableViewCell自己的drawRect中做到这一点。相反,创建一个自定义UIView并将其添加为子视图。

也是这个answer

查看是否所有youre做是试图设置的tableView的单元格的颜色,然后只需将cell's.ContentView设置为所需的颜色。

+0

我没有重载'UITableViewCell'的'drawRect'按我的最后一句话。我用一个子类'UIView'对象作为backgroundView进行测试。从来没有为该自定义视图调用'drawRect'。我不会搞乱单元的内部。 backgroundView是根本没有显示出来,我不知道为什么。 –

0

你的代码正在为我工​​作(我得到一堆黄色单元格),但只有当我返回numberOfRowsInSection:你确定这是工作?否则,我得到空的白色单元格,因为我告诉TableView没有任何显示。

例如

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return 10; 
}