NSInternalInconsistencyException与Xcode 9中的遗留ObjC代码

时间:2018-01-17 03:27:29

标签: ios objective-c xcode legacy-code

我刚开始学习swift,我似乎无法弄清楚如何修复我公司在2016年推出的音乐应用程序中的以下遗留代码。我正在努力更新平台,但应用程序每次我选择我的个人资料时都会崩溃,所以我认为它在UITableViewCell的某个地方,但是,由于以下因素推断出这一点:

  

"断言失败 - [UITableView   _dequeueReusableCellWithIdentifier:forIndexPath:usingPresentationValues:],   /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3698.33.7/UITableView.m:7883   2018-01-16 21:41:01.168538-0500 Gleamy [4101:1978203] ***终止   应用程序由于未捕获的异常' NSInternalInconsistencyException',   原因:'尝试为同一索引路径出列多个单元格,   这是不允许的。如果你真的需要将更多的细胞出列而不是   表视图正在请求,使用   dequeueReusableCellWithIdentifier:方法(没有索引路径)。   单元标识符:单元格,索引路径:   {length = 2,path = 0 - 0}'

***首先抛出调用堆栈:

  

(0x181c3a364 0x180e80528 0x181c3a238 0x1825d57f4 0x18b51a44c   0x18b353570 0x100b53334 0x18b52bac4 0x18b52c028 0x18b50a8ac   0x18b2c133c 0x18b1edf00 0x185c7d998 0x185c81b20 0x185bee36c   0x185c15b90 0x185c169d0 0x181be1edc 0x181bdf894 0x181bdfe50   0x181affe58 0x1839acf84 0x18b25467c 0x100b7d2b8   0x18161c56c)libc ++ abi.dylib:以未捕获的异常终止   输入NSException"

线程1:信号SIGABRT:

int main(int argc, char * argv[]) {
@autoreleasepool {
    return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}

}

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


if (indexPath.row==0)
{
    HeaderCell *cell=[tableView dequeueReusableCellWithIdentifier:@"headercell" forIndexPath:indexPath];
    objectForKey:@"profile_pic"]  , @"cover_pic":[response objectForKey:@"cover_pic"],  @"follow_count":[response objectForKey:@"follow_count"],  @"following_count":[response objectForKey:@"following_count"]   ,   @"user_name":[response objectForKey:@"user_name"], @"name":[response objectForKey:@"name"],};


    NSMutableDictionary *data= response;

    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self  action:@selector(GalleryClicked)];
    tapGesture.numberOfTapsRequired = 1;
    [cell.img_gallery setUserInteractionEnabled:YES];
    [cell.img_gallery addGestureRecognizer:tapGesture];



    UITapGestureRecognizer *viewProfilepic = [[UITapGestureRecognizer alloc] initWithTarget:self  action:@selector(showProfilePic)];
    viewProfilepic.numberOfTapsRequired = 1;
    [cell.img_profilepic setUserInteractionEnabled:YES];
    [cell.img_profilepic addGestureRecognizer:viewProfilepic];

    UITapGestureRecognizer *viewcoverpic = [[UITapGestureRecognizer alloc] initWithTarget:self  action:@selector(showCoverPic)];
    viewcoverpic.numberOfTapsRequired = 1;
    [cell.img_coverPic setUserInteractionEnabled:YES];
    [cell.img_coverPic addGestureRecognizer:viewcoverpic];



    [cell.btn_message addTarget:self action:@selector(myMessages) forControlEvents:UIControlEventTouchUpInside];
    [cell.btn_notification addTarget:self action:@selector(notifications) forControlEvents:UIControlEventTouchUpInside];

        [cell setUpHeaderCellWithData:data];


    if ([data objectForKey:@"profile_pic"])
    {
        if ([[data objectForKey:@"profile_pic"] containsString:@"http"])
            profilPicUrl=[data objectForKey:@"profile_pic"];
        else
            profilPicUrl=@"http://gleamedm.com/reb/img/fans/profile_pic/default.jpg";
    }


    if ([data objectForKey:@"cover_pic"])
    {
        if ([[data objectForKey:@"cover_pic"] containsString:@"http"])
            coverPicUrl=[data objectForKey:@"cover_pic"];

        else
            coverPicUrl=[NSString stringWithFormat:@"http://gleamedm.com/reb/img/fans/cover_pic/%@",[data objectForKey:@"cover_pic"]];
    }
    else
        coverPicUrl=@"http://gleamedm.com/reb/img/fans/cover_pic/default.jpg";




    return cell;
}
else
{
    int index=(int)indexPath.row-1;    // bcoz 0 is for header
    indexPath=[NSIndexPath indexPathForRow:index inSection:0];

    SinglePost *postObj = (SinglePost *)[posts objectAtIndex:index];

    if (postObj.msg_postImgStr .length >0)
    {
        MyProfileDataCell2 *cell=(MyProfileDataCell2*)[tableView dequeueReusableCellWithIdentifier:@"dataCell2" forIndexPath:indexPath];

        [cell setupCellWithPostObject:postObj];
        [cell.textView setDelegate: self];
        cell.textView.tag=index;


        UITapGestureRecognizer *tapGesture2 = [[UITapGestureRecognizer alloc] initWithTarget:self  action:@selector(Postclicked:)];
        [cell.topView setUserInteractionEnabled:YES];
        [cell.topView addGestureRecognizer:tapGesture2];
        [cell.topView setTag:index];


        [cell.btn_comment addTarget:self action:@selector(commentClicked:) forControlEvents:UIControlEventTouchUpInside];
        [cell.btn_likeUnlike addTarget:self action:@selector(likeUnlikeClicked:) forControlEvents:UIControlEventTouchUpInside];

        cell.btn_likeUnlike.tag=cell.btn_comment.tag=index;

        UITapGestureRecognizer *viewcoverpic = [[UITapGestureRecognizer alloc] initWithTarget:self  action:@selector(PostPictureClicked:)];
        viewcoverpic.numberOfTapsRequired = 1;
        [cell.img_messagePic setUserInteractionEnabled:YES];
        [cell.img_messagePic addGestureRecognizer:viewcoverpic];
        [cell.img_messagePic setTag:index];


        return cell;
    }
    else
    {
        DataCell1 *cell=[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        [cell setupCellWithPostObject:postObj];

        [cell.textView setDelegate: self];
        cell.textView.tag=index;

        UITapGestureRecognizer *tapGesture2 = [[UITapGestureRecognizer alloc] initWithTarget:self  action:@selector(Postclicked:)];
        [cell.topView setUserInteractionEnabled:YES];
        [cell.topView addGestureRecognizer:tapGesture2];
        [cell.topView setTag:index];


        [cell.btn_comment addTarget:self action:@selector(commentClicked:) forControlEvents:UIControlEventTouchUpInside];
        [cell.btn_likeUnlike addTarget:self action:@selector(likeUnlikeClicked:) forControlEvents:UIControlEventTouchUpInside];

        cell.btn_likeUnlike.tag=cell.btn_comment.tag=index;
        return cell;
    }
}

}

1 个答案:

答案 0 :(得分:0)

由于这些原因:

int index=(int)indexPath.row-1;    // bcoz 0 is for header
indexPath=[NSIndexPath indexPathForRow:index inSection:0];

当使用indexPath 0-1调用cellForRowAt方法时,代码中的dequeue调用使用indexPath 0-0,其中一个单元格已经出列(当使用indexPath 0-0调用cellForRowAt时)。因此错误。 (我只能假设这个内部一致性检查在最近的iOS版本中是新的,并且在首次编写代码时不存在。)

代码似乎没有使用indexPath,除了dequeue调用,所以我认为你可以删除上面的第二行:你应该保留第一行,因为它将你的数组索引重新映射到正确的行,到允许第0行的标题。