tableview cell 的重复添加

当有两种cell的时候,发生了如下神奇事件:

原因:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UserInfoCell *Cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_UserInfoCell forIndexPath:indexPath];

    PDOptionCell *optionCell = [tableView dequeueReusableCellWithIdentifier:optionCellId forIndexPath:indexPath];

    if (indexPath.section == 0) {
        switch (indexPath.row) {
            case 0:
                [Cell setUserInfoWithImage:@"Mine_rec_name" Title:@"真实姓名" Info:_login_User.cur_User.realname];
                return Cell;
                break;
            case 1:
                [Cell setUserInfoWithImage:@"Mine_rec_num" Title:@"身份证号码" Info:_login_User.cur_User.id_card];
                return Cell;
                break;
            case 2:
            {
//                NSIndexPath *index = [NSIndexPath indexPathForRow:2 inSection:0];
                [optionCell reconstructSubviewsUnderMode:OptionCellModePic];
//                [self.myTableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:index] withRowAnimation:UITableViewRowAnimationAutomatic];
                return optionCell;
            }
                break;
        }

调用了

dequeueReusableCellWithIdentifier:   forIndexPath:

在重用机制里,这个方法会重复调用两次

导致重用出现错乱,在只有一种cell的时候,察觉不到这样的情况,

但是一旦有多种cell,一定导致重用出错!

于是解决办法:换用

dequeueReusableCellWithIdentifier:

方法

结果如下:

时间: 2024-08-07 15:34:39

tableview cell 的重复添加的相关文章

Warning once only: Detected a case where constraints ambiguously suggest a height of zero for a tableview cell's content view...

Warning once only: Detected a case where constraints ambiguously suggest a height of zero for a tableview cell's content view. We're considering the collapse unintentional and using standard height instead. 遇到以上错误, 添加以下代码可以解决: self.tableView.rowHeigh

【iOS知识学习】_iOS动态改变TableView Cell高度

在做tableView的时候,我们有时候需要根据cell的高度动态来调整,最近在网上看到一段代码不错,跟大家Share一下. 在 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 类中获取cell的高度: CGSize boundSize = CGSizeMake(216, CGFLOAT_MAX); cell.textLabel.text

iOS tableview cell 的展开收缩

iOS tableview cell 的展开收缩 #import "ViewController.h" @interface ViewController ()<UITableViewDataSource,UITableViewDelegate>{ NSMutableArray *_allArray;//创建一个数据源数组 NSMutableDictionary *dic;//创建一个字典进行判断收缩还是展开 } @property (nonatomic,strong)UI

[IOS Tableview] cell自定义view显示错误问题

问题介绍:按照tableviewcell的tag自定义cell的view显示的时候,会出现拖动时显示错误情况. 我做的是一个下载界面,我为了简化问题,就把问题设定为,tag==1的cell已下载,加载时就把已下载的cell的label显示为蓝色.其余默认为黑. 比如我在代码里,想要tag==1的cell的label字体为蓝色,这样写就会出现上下拖动时tag==11的也出现蓝色(视具体情况而定). if([cell.tag==1){ //tag==1就把label显示为蓝色 cell.label.

数据库表中不建索引,在插入数据时,通过sql语句防止重复添加

sql 语句 INSERT IGNORE INTO table(aaa,bbb) SELECT '1111','2222' FROM DUAL WHERE NOT EXISTS( SELECT id FROM table WHERE bbb= '2222' ) mybatis 中代码 <insert id="addItemCharacteristic" parameterType="com.orderalliance.entity.CharacteristicDTO&q

ios开发 在cell中动态添加图片解决重复出现图层问题

1.在cell初始化的时候创建scrollView,然后往scrollView中添加imageView,最后在重用cell的时候动态计算scrollView的高度 总而言之,就是初始化创建控件要放在cell的init里面,赋值放init外面,不然每次循环都会重复创建imageView视图 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UI

破船哥的给tableview cell添加动画

首先假设你已经能够熟练使用UITableView了.那么我们只需要实现UITableViewDelegate中的tableView:WillDisplayCell:ForRowAtIndexPath:即可.当cell显示之前,会先调用该方法,因此给cell添加动画,在这个方法里面即可.(瞬间爆炸) -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:

在tableview的headerView中添加webView,webView自适应高度

最近在项目中需要添加一个webView加载的页面,下面显示的是对这个webView所显示的内容的一个评论列表 ,列表要根据后台加载过来的HTML自适应的变化高度,tableview的cell在webView的下面显示,显示的效果 1.设置webView ,初始的高度设置为0.5 ,设置为tableHeaderView为webView,添加了一个观察者,监控webView 的contentSize的变化 _myWebView = [[UIWebView alloc]initWithFrame:CG

tableView cell性能优化

通过一个标识表去缓冲池中寻找可循环利用的cell 如果缓存池找不到可循环利用的cell,创建一个新的cell,给cell贴个标识 给cell设置新的数据 代码如下cellForRowAtIndexPath方法中 //dequeue查找队列 //cell标识,static修饰局部变量:可以保证局部变量只分配一次存储空间 static NSString *ID = @"A"; UITableViewCell *cell = [tableView dequeueReusableCellWit