我正在尝试编写代码来根据它在表格中的位置来修改单元格的背景颜色.虽然以下代码"有效",但它只会影响传递给它的单元格.空单元格不受影响.
- (void)tableView: (UITableView*)tableView willDisplayCell: (UITableViewCell*)cell forRowAtIndexPath: (NSIndexPath*)indexPath
{
if(!indexPath.row%2)
{
cell.backgroundColor = [UIColor colorWithRed: 0.0 green: 0.0 blue: 1.0 alpha: 1.0] ;
NSLog(@"Blue");
}
else{
cell.backgroundColor = [UIColor whiteColor];
NSLog(@"white");
}
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
}
Run Code Online (Sandbox Code Playgroud)
如果列表中的项目很少,我如何影响显示的其余单元格(如果为空)?
通过"空"单元格,我的意思是当数据源没有足够的数据来填充给予表视图的整个视图时显示的占位符单元格.例如,如果你可以容纳10个单元格,但只有5个数据,那么你只需要处理5个单元格 - 接下来的5个是空的,据我所知,无法访问.
你应该实施
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Run Code Online (Sandbox Code Playgroud)
在您的委托中并根据需要初始化单元格。