iOS 自定义UItableViewCell.

本文介绍如何在iOS应用中自定义UITableViewCell。通过实例演示了从创建视图开始到自定义单元格标识符、设计单元格样式及加载不同状态背景图片的全过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、首先新建View

2、将Table View cell 控件拖入View 

3、自定义Table View Cell 的identifier 

4、设计自己的Table View Cell

5、在UItableView 方法中声明使用自己自定义的Cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CustomCellIdentifier = @"CustomCellIdentifier";
   //CustomCellIdentifier 为自己自定义的cell identifier
    static BOOL nibsRegistered = NO;
    if (!nibsRegistered) {
        
        UINib *nib;        
        if ([[UIDevice currentDevice]userInterfaceIdiom]==UIUserInterfaceIdiomPad) {
            nib=[UINib nibWithNibName:@"UIDownLoadCell_ipad" bundle:nil];
        }else{
            nib=[UINib nibWithNibName:@"UIDownLoadCell_iphone" bundle:nil];
        }
    [tableView registerNib:nib forCellReuseIdentifier:CustomCellIdentifier];        
        nibsRegistered = YES;
    }
    
    UIDownLoadCell *cell = [tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];  
//分别设置自己在Cell 中添加的控件。 
    [cell.areaname setText:[self.areanames objectAtIndex:indexPath.row]];
    [cell.progress setHidden:YES ];
    NSString * url=[NSString stringWithFormat:@"%@%@",hostUrl,[self.downLoadUrl objectAtIndex:indexPath.row]];
    cell.downloader=[[DWDownloader alloc]initWithPerpeties:url localFile:[self.downLoadUrl objectAtIndex:indexPath.row] threadCount:1];
    [cell.downLoad addTarget:cell action:@selector(startDownLoad:) forControlEvents:UIControlEventTouchUpInside];    
    [self.allCells setObject:cell forKey:[self.downLoadUrl objectAtIndex:indexPath.row]];
    
    if ([cell.areaname.text hasPrefix:@"浙江省"]&&[cell.areaname.text hasSuffix:@")"]) {
        [cell.downLoad setBackgroundImage:self.hasDownLoadImg forState:UIControlStateNormal];
        [cell.downLoad setEnabled:NO];
    }else if(![cell.areaname.text hasPrefix:@"浙江省"]&&[cell.areaname.text hasSuffix:@")"]){
        [cell.downLoad setBackgroundImage:self.deleteImgImg forState:UIControlStateNormal];    
    }
    return cell;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值