[课堂实践与项目]Cell总结:UITableViewCell与自定义Cell的几种显示方式(含交叉cell绘制)

这篇博客总结了iOS开发中UITableViewCell和自定义Cell的创建和显示方法。内容包括通过xib文件和代码生成两种加载方式,以及系统cell的不同样式。此外,还详细介绍了如何实现不同类型的Cell在同一个TableView中的交叉绘制。

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

今天学习了系统cell和自定义的cell。并且使用了nib文件和代码两种方式建立cell。我就顺势来总结下cell吧。

从cell的类型来说,分 系统cellUITableVIewCell和自定义MyCell两种。

cell的加载方式来说,可以通过 xib文件、代码生成两种方式。

cell的显示方式来说:分为一般的统一显示和一个tableView两种不同的cell显示方式。

一。UITableViewCell的cell。

1.xib文件的加载,代码生成。由于系统的cell。上篇就介绍了系统cell的属性。直接给出协议的cell绘制方式吧

d

1.UITableViewCellStyleSubtitle 类型

2.

UITableViewCellStyleDefault

3.

UITableViewCellStyleValue1

4.

UITableViewCellStyleValue2

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

    static NSString *cellIndentifiter = @"CellIndentifiter";
 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifiter];
    
    if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIndentifiter];
    }
    
        cell.imageView.image = [UIImage imageNamed:@"1.png"];
        cell.textLabel.text = @"cu1";
        cell.detailTextLabel.text = @"detail tet1";
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        return cell;
}

二,自定义cell的加载。

1.xib文件的加载。

1)我们事先定义了cell的xib,h,m文件。

并在xib文件中绘制了cell的外观


2)下一步就是自定义cell的绘制啦


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

    static NSString *cellIndentifiter = @"MyIndentifiter";

        static BOOL yesOrNo = NO;
        if (!yesOrNo) {
            UINib *nib = [UINib nibWithNibName:@"LCTableVIewCell" bundle:nil];
            
            [tableView registerNib:nib forCellReuseIdentifier:cellIndentifiter];
        }

        LCTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifiter];
    
    cell.mLable1.text = @"lab1";
    cell.mLable1.text = @"lab2";
        
        return cell;
}

2.自定义cell的代码实现

1)自定义cell的内部实现就要看下面的这个方法啦,代码我就不写了。按照frame进行设置就ok

#import "LCTableViewCell.h"

@implementation LCTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        
        // Initialization code
    }
    return self;
}

效果是相同的。

2)还有一种我们常见的办法是在我们的系统cell里面,当cell==nil的时候,进行自定义cell绘制。

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

    static NSString *cellIndentifiter = @"CellIndentifiter";
 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifiter];
    
    if (cell == nil) {
           //cell的自定义绘制
    }
    
        //cell的属性设置
        
        return cell;
}



三,下面就是两种不同的cell类型的穿插绘制勒

如图:

代码如下:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
      NSInteger row = [indexPath row];
      NSLog(@"%d",row);
      static NSString *cellIndentifiter = @"CellIndentifiter";
    static NSString *cellIndentifiter1 = @"CellIndentifiter1";
     row = row%2;
    if (!row) {
       

            UINib *nib = [UINib nibWithNibName:@"LCTableVIewCell" bundle:nil];
            
            [tableView registerNib:nib forCellReuseIdentifier:cellIndentifiter1];
            
    
        LCTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifiter1];
        
        cell.mLable1.text = @"custom label1";
        cell.mLabel2.text = @"custom label2";
        
            return cell;
    }
    else
    {
        
        
        
        UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIndentifiter];
            
            
        
        cell.imageView.image = [UIImage imageNamed:@"1.png"];
        cell.textLabel.text = @"cu1";
        cell.detailTextLabel.text = @"detail tet1";
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
     return cell;
    }
    
   
   
}

交叉绘制的的注意点:

1)每一次的cell的绘制都是需要创建新的cell,为了避免两种不同的方式的交叉混乱的情况

2)对于什么时候绘制什么样的表格也需要设置清楚


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值