简体   繁体   English

从UITableView外部TableView委托中删除行

[英]Delete Row from UITableView Outside TableView Delegate

I am trying to delete a row from UITableview outside the delegate method. 我试图从委托方法之外的UITableview中删除一行。 I am calling a method when I click a button inside a table cell and trying to delete the row inside that method. 当我单击表单元格内的按钮并尝试删除该方法内的行时,我正在调用方法。 Here is the function I am using 这是我正在使用的功能

UIButton *btn = (UIButton*)sender;
int tag = btn.tag;
UITableViewCell *buttonCell = (UITableViewCell*)[[btn superview] superview];
NSIndexPath *indexPath = [self.msgTbl indexPathForCell:buttonCell];
[deleg.rmessages removeObjectAtIndex:buttonRow];
[self.msgTbl deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop];NSInteger buttonRow = indexPath.row;
[self.msgTbl reloadData];

Using this one or two rows get deleted but after that its crashing giving exception 使用此一两行会被删除,但此后崩溃会导致异常

Number of rows before and after deletion must be same 删除前后的行数必须相同

How can I do this in ios? 如何在iOS中执行此操作? Thanks 谢谢

Your problem is that the data that is being taken to populate your table isn't consistent with the table after deleting the cell. 您的问题在于,删除单元格后,用于填充表的数据与表不一致。 Make sure your dataSource methods provide the correct data after doing this (for example, if it is an array of objects you are using to populate the table, you must remove the object from the array as well) 执行此操作后,请确保您的dataSource方法提供正确的数据(例如,如果它是用于填充表的对象数组,则也必须从该数组中删除该对象)

The root issue is that this method: 根本问题是这种方法:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

Must return the correct number of rows, and it isn't. 必须返回正确的行数, 但不是。 When you remove the item from deleg.rmessages , is this the same object that is being used to supply the return value of the above method? deleg.rmessages删除项目时,这是否是用于提供上述方法的返回值的对象? (Something like [deleg.rmessages count] ?) (类似于[deleg.rmessages count]吗?)

Also, in my experience that exception often gives you more details, in particular: 另外,根据我的经验,该异常通常会为您提供更多详细信息,尤其是:

  1. How many items it had before 以前有多少个物品
  2. How many were added/deleted 添加/删除了多少个
  3. How many it expects to have vs. how many it does have after the reload 它有多少期望有与有多少重装后有

Do you see anything like this being mentioned? 您看到有人提到过这种情况吗? If so, it would be worth including in your question. 如果是这样,则值得在您的问题中提出。

Sidenote: 边注:

It's a bad idea to rely on: 依靠是一个坏主意:

UITableViewCell *buttonCell = (UITableViewCell*)[[btn superview] superview];

To return the UITableViewCell . 返回UITableViewCell You appear to assign the tag of the button to a local variable, but never use it. 您似乎将按钮的tag分配给了局部变量,但从未使用过它。 (Maybe this would be a good place to store the index of the UITableViewCell, and then subclass the cell to maintain an ivar to the button?) This is only part of the problem. (也许这是存储UITableViewCell的索引,然后将其子类化以维护按钮的ivar的好地方吗?)这只是问题的一部分。

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

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