IOS自定义UITableViewCell

本文详细解析如何在iOS开发中自定义UITableView的Cell样式,包括自定义图片、文本信息等,并通过实例代码展示实现过程。

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

在用到UITableVIew的时候,经常会自定义每行的Cell

IOS控件UITableView详解中的下面代码修改部分代码就可以实现自定义的Cell了

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

自定义代码:

static NSString *CellWithIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellWithIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellWithIdentifier];
    }
    
    
    NSUInteger row = [indexPath row];
    // 自定义Cell中Image
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 24, 24, 24)];
    imageView.image = [UIImage imageNamed:@"green.png"];
    [cell.contentView addSubview:imageView];
    [imageView release];
   
    // 自定义文本信息
    UILabel *city = [[UILabel alloc] initWithFrame:CGRectMake(50, 25, 100, 20)];
    NSString *cityString = [[NSString alloc] initWithFormat:@"城市:%@",[self.dataList objectAtIndex:row]];
    city.text = cityString;
    [cell.contentView addSubview:city];
    [cityString release];
    
//    cell.textLabel.text = [self.dataList objectAtIndex:row];
//    cell.imageView.image = [UIImage imageNamed:@"green.png"];
//    cell.detailTextLabel.text = @"详细信息";
//    cell.accessoryType = UITableViewCellSelectionStyleGray;


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值