gpt4 book ai didi

ios - 删除最后一个 UITableViewCell 时出错

转载 作者:行者123 更新时间:2023-11-29 02:39:56 24 4
gpt4 key购买 nike

我有以下代码来填充我的表格 View :

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (friendsArray.count > 0)
{
return friendsArray.count;
}
return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}
if (friendsArray.count <= 0)
{
cell.textLabel.text = @"It is lonely here";
cell.detailTextLabel.text = @"Please add some friends";
}
else
{
Friend *friend = [friendsArray objectAtIndex:indexPath.row];
cell.textLabel.text = friend.name;
cell.detailTextLabel.text = friend.email;
}
return cell;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationTop];
FriendDelete *friendDelete = [FriendDelete new];
friendDelete.FDDelegate = self;
Friend *friend = [friendsArray objectAtIndex:indexPath.row];
[friendDelete deleteFriend:friend.requestId];
[app showLoader:@"Deleting.."];
}
}

为了删除委托(delegate)调用中的 friend ,我写了:

- (void)friendDeleteSuccessfull:(NSString *)userId
{
[app hideLoader];
[app dispToast:@"Deleted Friend"];
for (Friend *friend in friendsArray)
{
if ([friend.userId isEqualToString:userId])
{
NSInteger row = [friendsArray indexOfObject:friend];
[friendsArray removeObjectAtIndex:row];
[friendListTable endUpdates];
[friendListTable reloadData];
break;
}
}
}

在我到达最后一行之前一切正常。当我尝试删除表格的最后一行时,应用程序崩溃并出现以下错误:

Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-3318/UITableView.m:1582

因此更改了代码,以便仅当数组有任何值时调用 endUpdates,否则通过添加以下条件直接调用 reloadData:

if (friendsArray.count > 0) {
[friendListTable endUpdates];
}

现在最后一行删除没有崩溃,但表格仍然显示最后一行。也就是说,该表根本没有重新加载。可能是什么问题。我该如何解决?

最佳答案

尝试在

中写入 [tableView reloadData]
if(friendsArray.count > 0){
[friendListTable endUpdates];
[tableView_object reloadData];
}

关于ios - 删除最后一个 UITableViewCell 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25915243/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com