iphone - uitableviewcell 交替背景单元格颜色,即使对于没有数据的单元格也是如此

标签 iphone objective-c ios uitableview

我知道如何为 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/

相关文章:

objective-c - 自定义 iPhone 应用程序任务栏

iphone - ios - UICollectionView 在水平分页时更改默认偏移量

iphone - ios:如何将 NSString 转换为 int64_t?

ios - 在 SWIFT 中从 iPhone 获取联系电话时应用程序崩溃

iphone - CoreData 基础知识 – 多对多关系数组数据

android - 模拟器 : WARNING: HAXM 6. 0.5 已安装。请安装 HAXM >= 6.2.1 以修复 Mac 上的兼容性问题

ios - UISplitViewController 滑动手势干扰其他滑动手势

objective-c - 如何为多个子类使用单个 Storyboard uiviewcontroller

ios - 更改键盘返回键标签文本

ios - 使用 S3 下载功能异步快速更新 tableview