uitableviewcell交替背景单元格颜色甚至是没有数据的单元格

时间:2011-10-07 20:54:58

标签: iphone objective-c ios uitableview

我知道如何将交替颜色设置为tableviewcell,但是如何将所有可见行设置为具有交替颜色?例如,现在如果我只有2个数据单元格,只有2个单元格会有背景颜色 - 我如何填充空单元格的背景?

2 个答案:

答案 0 :(得分: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)Alternare your cell:

-(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)in 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; }

答案 1 :(得分:0)

将虚拟单元格添加到列表的末尾。