UITableViewCell awakeFromNib

在使用UITableViewCell时,可以通过awakeFromNib方法进行初始化设置。对于纯代码的方式,需在tableViewCell类中实现initWithStyle:reuseIdentifier:方法,并在tableView的数据源方法中替换默认的UITableViewCell。若采用xib方式,需新建一个空的User Interface文件,添加tableViewCell控件,并在代码中注册cell。确保.m, .h, .xib文件名相同,并在适当位置注册cell。" 128470457,5564205,华为OD机试:Python实现硬件产品销售组合,"['华为机试', '编程题', '算法', '回溯', 'Python实现']

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

纯代码

则tableViewCell类中需实现 initWithStyle:reuseIdentifier:

@implementation MyCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        //创建自定义单元格中控件
        self.myImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 120, 120)];
        UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(160, 45, 51, 21)];
        nameLabel.text = @"姓名:";
        self.nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(239, 45, 51, 21)];
        UILabel *nicknameLabel = [[UILabel alloc]initWithFrame:CGRectMake(160, 80, 51, 21)];
        nicknameLabel.text = @"昵称:";
        self.nicknameLabel = [[UILabel alloc]initWithFrame:CGRectMake(239, 80, 51, 21)];
        //加入单元格内容视图中
        [self.contentView addSubview:self.myImageView];
        [self.contentView addSubview:nameLabel];
        [self.contentView addSubview:self.nameLabel];
        [self.contentView addSubview:nicknameLabel];
        [self.contentView addSubview:self.nicknameLabel];
    }
    return self;
}

调用该cell的tableView中只需在下面方法中将系统默认的UITableViewCell替换成自己的类

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //创建可重用标识符
    static NSString *cellID = @"cellID";
    //首先从出列可重用队列中获取单元格
    MyCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    //如果没有则创建单元格
    if (!cell) {
        cell = [[MyCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    //获取Student对象
    Student *stu = self.students[indexPath.row];
    // Configure the cell...
    //设置自定义单元格的内容
//    cell.myImageView.image = stu.icon;
//    cell.nameLabel.text = stu.name;
//    cell.nicknameLabel.text = stu.nickname;
    [cell bind:stu];//绑定数据
    return cell;
}

纯xib

新建newFile-userInterface-empty

然后拖入tableViewCell控件。

实现代码如下。其中 if (!isRegister)部分可以放在viewDidLoad中。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //创建可重用标识符
    static NSString *cellID = @"cellID";
    //注册xib,生成单元格对象,只需注册一次就放入出列可重用队列中,以后直接从出列可重用队列中获取此单元格对象即可
    static BOOL isRegister = NO;//是否注册过标识
    if (!isRegister) {//如果没有注册过,则注册
        UINib *nib = [UINib nibWithNibName:@"MyCell" bundle:nil];
        [tableView registerNib:nib forCellReuseIdentifier:cellID];
        isRegister = YES;//设置注册标志位
    }
    //从出列可重用队列中获取单元格
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
    //获取Student对象
    Student *stu = self.students[indexPath.row];
    // Configure the cell...
    //设置自定义单元格的内容
    UIImageView *imageView = (UIImageView *)[cell viewWithTag:100];
    imageView.image = stu.icon;
    UILabel *nameLabel = (UILabel *)[cell viewWithTag:101];
    nameLabel.text = stu.name;
    UILabel *nickNameLabel = (UILabel *)[cell viewWithTag:102];
    nickNameLabel.text = stu.nickname;
    return cell;
}


代码+xib

.m .h .xib三者取同样的名字

cell中实现

<span class="s3">//</span><span class="s14">代码加</span><span class="s3">xib</span><span class="s14">混合</span>
- (void)awakeFromNib {
    // Initialization code
    self.backgroundColor=[UIColor greenColor];
    _btn.backgroundColor=[UIColor orangeColor];
    
}

调用该cell 的tableView中实现

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerNib:[UINib nibWithNibName:@"TableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 10;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellID = @"cell";
    //首先从出列可重用队列中获取单元格
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    //如果没有才创建单元格对象
    if (!cell) {
        cell = [[TableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    NSLog(@"%@",NSStringFromCGRect(cell.contentView.frame));
    NSLog(@"%@",NSStringFromCGRect( cell.frame));
    
    return cell;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值