一个UITableView两种Cell的实现

本文介绍如何在一个UITableView中,通过SegmentedControl切换显示两种Cell:一种仅显示标题,另一种带备注。关键在于为不同类型的Cell设置不同的identifier。

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

在一个页面中只有一个UITableView,用一个SegmentedControl来控制显示两个list。其中一个list只想让它显示标题,另外一个则带有备注。之前总是两个list都只能显示标题,后来发现种方法,主要思想是为两种cell使用不同的identifier,记录下来以备后用,顺便也明白了identifier是干这个用的。

在(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 方法中做就可以了。


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableViewIndex==0)
    {
        static NSString *personTableIdentifier=@"personTableIdentifier";
        
        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:personTableIdentifier];
        if(cell==nil)
        {
            cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:personTableIdentifier];
        }
        
        NSInteger row=[indexPath row];
        cell.textLabel.text=[account.relativePerson objectAtIndex:row];
        
        return  cell;
    }
    else
    {
        static NSString *itemTableIdentifier=@"itemTableIdentifier";
        
        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:itemTableIdentifier];
        if(cell==nil)
        {
            cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:itemTableIdentifier];
        }
        
        NSInteger row=[indexPath row];
        cell.textLabel.text=[[account.items objectAtIndex:row]toNSString];
        cell.detailTextLabel.text=[[account.items objectAtIndex:row]description];
        cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
        
        return  cell;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值