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:

方法

 

结果如下:

 

 

posted @ 2016-07-05 19:33  BarnettWu  阅读(390)  评论(0编辑  收藏  举报