xcode - iOS tableView cell复用出错问题

 mobiledu2502878025 发布于 2022-10-30 09:58

部分代码如下,点击后,刷新cell都出错,复用出了问题,请问要怎么设置复用,如何避免这样的问题

static NSString * const defealtSelectCellID = @"applyTransferEstSelectItemCell";

[_tableView registerNib:[UINib nibWithNibName:@"ApplyTransferEstSelectItemCell" bundle:nil]
         forCellReuseIdentifier:defealtSelectCellID];

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.row) {
        case 0:{

            ApplyTransferEstSelectItemCell *cell = [tableView dequeueReusableCellWithIdentifier:defealtSelectCellID forIndexPath:indexPath];
            
            if (_propertyFollowAllAddEntity.TrustorTypeKeyId) {
                cell.rightValueLabel.text = _propertyFollowAllAddEntity.TrustorTypeKeyId;
            }else{
                [cell setupLeftTitleWithString:@"*联系人类型" rightLabelString:@"请选择联系人类型"];
            }
            
            return cell;
        }
case 2:{

            ApplyTransferEstSelectItemCell *cell = [tableView dequeueReusableCellWithIdentifier:defealtSelectCellID forIndexPath:indexPath];
            
            
            if (_propertyFollowAllAddEntity.TrustorGenderKeyId) {
                cell.rightValueLabel.text = _propertyFollowAllAddEntity.TrustorGenderKeyId;
            }else{
                [cell setupLeftTitleWithString:@"*性别" rightLabelString:@"请选择联系人性别"];
            }
            
            
            return cell;
        }

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    _selectedIndexPath = indexPath;
    
    switch (indexPath.row) {
        case 0:{            
                NSIndexPath *reloadIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
                [weakSelf.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:reloadIndexPath]
                                          withRowAnimation:UITableViewRowAnimationNone];
                
            }];
            break;
            
        }
        case 2:{
                NSIndexPath *reloadIndexPath = [NSIndexPath indexPathForRow:2 inSection:0];
                [weakSelf.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:reloadIndexPath]
                                          withRowAnimation:UITableViewRowAnimationNone];
                
            }];
            break;
            
        }
2 个回答
  • 结合cell缓冲池和prepareForReuse方法,可参考博客http://serazheng.github.io/2015-08-15-%E...

    2022-11-12 01:39 回答
  • 要考虑从复用队列找不到cell的情况

    2022-11-12 01:39 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有