简体   繁体   English

行删除不会刷新iOS应用程序中的表格视图

[英]Row deletion does not refresh table view in ios app

I have spent hours searching for the solution with out any luck. 我花了数小时没有任何运气寻找解决方案。 I am trying to delete a row (also deselect same row) programmatically. 我正在尝试以编程方式删除一行(也取消选择同一行)。 After row deletion call below, UITableViewDelgate methods get called expectedly and data source is updated but UITableView is not refreshed. 在下面的行删除调用之后,将按预期方式调用UITableViewDelgate方法,并且将更新数据源,但不会刷新UITableView。 deselectRowAtIndexPath call also does not work. deselectRowAtIndexPath调用也不起作用。 I tried all kinds of scenarios as shown by commented lines. 我尝试了各种情况,如注释行所示。

Here is my code: checkoutPerson is called as a result of observer listening for NSNotificationCenter messages. 这是我的代码:由于观察者侦听NSNotificationCenter消息而调用checkoutPerson。

- (void) checkoutPerson: (NSNumber*) personId {
Person *person = [_people objectForKey:personId];
if( person )
{
    // Remove person from data source
    int rowIndex = person.rowIndex;
    S2Log(@"Deleting row number=%d", rowIndex);
    [_allKeys removeObjectAtIndex:rowIndex];
    [_people removeObjectForKey: personId];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:rowIndex inSection:0];

    //[[self tableView] beginUpdates];
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    S2Log(@"Deleting indexPath row=%d", [indexPath row]);
    [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                    withRowAnimation:UITableViewRowAnimationFade];
    //[[self tableView] endUpdates];
    S2Log(@"Reloading data");
    //[[self tableView] reloadData];
    //[self performSelector:@selector(refreshView) withObject:nil afterDelay:1.5];
    //[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES];
}

} }

I will appreciate for help. 感谢您的帮助。 Thanks -Virendra 谢谢-Virendra

I believe deleted cell is not being recycled. 我相信已删除的单元格不会被回收。 If I delete row in the middle, last row is always erased (since there is one less item) but the deleted row remains. 如果我删除中间的行,则总是删除最后一行(因为少了一项),但是删除的行仍然保留。

Use the above code between two function for table view 在表格视图的两个函数之间使用以上代码

[tableView beginUpdates];

// the deletion code from data source and UITableView

[tableView endUpdates];

By calling this functions you are telling UITableView that you are about to make updates for deleting your cell. 通过调用此函数,您将告诉UITableView您将要进行更新以删除单元格。

Edit 编辑

The other problem I see with your code is you first delete the data from the data source. 我在代码中看到的另一个问题是,您首先要从数据源中删除数据。

Now you are asking for the UITableViewCell (which actually reloads the UITableView ) and then you are deleting the row from UITableView 现在,您要获取UITableViewCell (实际上是重新加载UITableView ),然后从UITableView中删除该行。

I guess you should fetch the UITableViewCell before deleting values from your data source. 我想您应该先删除UITableViewCell然后再从数据源中删除值。

I found the problem. 我发现了问题。 It has nothing to do with the code I posted above. 它与我上面发布的代码无关。 It is syncing problem between visual display and the contents of data source. 这是视觉显示和数据源内容之间的同步问题。 I have an embedded UITableView as part of a composite view. 我有一个嵌入式UITableView作为复合视图的一部分。 In composite view's controller, I was wiring up UITableView's delegate and data source to an instance of UITableViewController. 在复合视图的控制器中,我将UITableView的委托和数据源连接到UITableViewController的实例。 Instead of this, I should have set UITableViewController's tableView property to the embedded UITableView. 取而代之的是,我应该将UITableViewController的tableView属性设置为嵌入式UITableView。 It seems that UITableView has to be contained within UITableViewController in order to correctly sync up table view visual display to the contents of data source. 似乎UITableView必须包含在UITableViewController中,以便正确地将表视图的可视显示同步到数据源的内容。 This also fixes row deselection and scrolling. 这也解决了行取消选择和滚动的问题。 I also needed to delay reloadData call in which case deleteRowsAtIndexPaths:withRowAnimation is not required. 我还需要延迟reloadData调用,在这种情况下,不需要deleteRowsAtIndexPaths:withRowAnimation。 All you need is to modify the contents of your data source and call reloadData with a delay of 1.5 Seconds. 您所需要做的就是修改数据源的内容,并以1.5秒的延迟调用reloadData。

Thanks to all for great help. 感谢所有人的大力帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM