NSInternalInconsistencyException 与遗留 ObjC 代码 Xcode 9
NSInternalInconsistencyException with legacy ObjC code in Xcode 9
我刚刚开始学习 swift,我似乎无法弄清楚如何修复我公司在 2016 年推出的音乐应用程序中的以下遗留代码。我正在努力更新平台,但是每次我 select 我的个人资料时,应用程序都会崩溃,所以我假设它在 UITableViewCell 中的某个地方,但是,这是由于以下原因推断的:
"Assertion failure in -[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] *** Terminating
app due to uncaught exception 'NSInternalInconsistencyException',
reason: 'Attempted to dequeue multiple cells for the same index path,
which is not allowed. If you really need to dequeue more cells than
the table view is requesting, use the
dequeueReusableCellWithIdentifier: method (without an index path).
Cell identifier: cell, index path:
{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: terminating with uncaught exception of
type 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;
}
}
}
因为这些行:
int index=(int)indexPath.row-1; // bcoz 0 is for header
indexPath=[NSIndexPath indexPathForRow:index inSection:0];
当使用 indexPath 0-1 调用 cellForRowAt 方法时,代码中的出队调用使用的是 indexPath 0-0,其中一个单元格已经出队(当使用 indexPath 0-0 调用 cellForRowAt 时)。因此错误。 (我只能假设这种内部一致性检查是 iOS 的最新版本中的新功能,并且在首次编写代码时不存在。)
代码似乎没有使用 indexPath 除了 dequeue 调用,所以我认为你可以删除上面的第二行:你应该保留第一行,因为它将你的数组索引重新映射到正确的行, 以允许第 0 行的 header.
我刚刚开始学习 swift,我似乎无法弄清楚如何修复我公司在 2016 年推出的音乐应用程序中的以下遗留代码。我正在努力更新平台,但是每次我 select 我的个人资料时,应用程序都会崩溃,所以我假设它在 UITableViewCell 中的某个地方,但是,这是由于以下原因推断的:
"Assertion failure in -[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] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Attempted to dequeue multiple cells for the same index path, which is not allowed. If you really need to dequeue more cells than the table view is requesting, use the dequeueReusableCellWithIdentifier: method (without an index path). Cell identifier: cell, index path: {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: terminating with uncaught exception of type 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;
}
}
}
因为这些行:
int index=(int)indexPath.row-1; // bcoz 0 is for header
indexPath=[NSIndexPath indexPathForRow:index inSection:0];
当使用 indexPath 0-1 调用 cellForRowAt 方法时,代码中的出队调用使用的是 indexPath 0-0,其中一个单元格已经出队(当使用 indexPath 0-0 调用 cellForRowAt 时)。因此错误。 (我只能假设这种内部一致性检查是 iOS 的最新版本中的新功能,并且在首次编写代码时不存在。)
代码似乎没有使用 indexPath 除了 dequeue 调用,所以我认为你可以删除上面的第二行:你应该保留第一行,因为它将你的数组索引重新映射到正确的行, 以允许第 0 行的 header.