我知道如何为 tableviewcell 设置交替颜色,但如何将所有可见行设置为交替颜色?例如,现在如果我只有 2 个包含数据的单元格,那么只有 2 个单元格会有背景色 - 我如何也填充空单元格的背景?
最佳答案
尝试做这样的事情: (a) 仅添加所需数量的虚拟行 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if ([UIAppDelegate.getwaitlistArray count]>=numberOfVisibleRowsOnYourScreen) {
return [UIAppDelegate.getwaitlistArray count];
}
else{
return ([UIAppDelegate.getwaitlistArray count] + numberOfVisibleRowsOnYourScreen - [UIAppDelegate.getwaitlistArray count]);
}
(b)改变你的手机:
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
//alternating cell back ground color
if (indexPath.row%2 == 0) {
[cell setBackgroundColor:[UIColor colorWithRed:237.0/255.0f green:237.0/255.0f blue:237.0/255.0f alpha:1.0f]];
}else
{
[cell setBackgroundColor:[UIColor colorWithRed:247.0/255.0f green:247.0/255.0f blue:247.0/255.0f alpha:1.0f]];
}
(c)在 cellForRowAtIndexPath 中:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyIdentifier"] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
//Create your lables/buttons/text/image
nameLabel.test=@"Whatever"; ..... ......
……
if (indexPath.row < [YourArray count]) { Populate your data here in the cells } else { cell.userInteractionEnabled=FALSE; nameLabel.text=@""; } return cell; }
关于iphone - uitableviewcell 交替背景单元格颜色,即使对于没有数据的单元格也是如此,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7692773/