从 TableView 委托之外的 UITableView 中删除行
我试图从委托方法之外的 UITableview 中删除一行。当我单击表格单元格内的按钮并尝试删除该方法内的行时,我正在调用一个方法。 这是我正在使用的函数,
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];
使用这一行或两行被删除,但之后它崩溃并给出异常
删除前后的行数必须相同
在ios中如何做到这一点? 谢谢
I am trying to delete a row from UITableview outside the delegate method. 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?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的问题是,删除单元格后填充表格的数据与表格不一致。执行此操作后,请确保您的 dataSource 方法提供正确的数据(例如,如果它是您用来填充表的对象数组,则还必须从数组中删除该对象)
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)
根本问题是此方法:
必须返回正确的行数,但事实并非如此。当您从
deleg.rmessages
中删除项目时,这是否相同用于提供上述方法的返回值的对象? (类似于[deleg.rmessages count]
?)此外,根据我的经验,异常通常会为您提供更多详细信息,特别是:
你看到有人提到类似的内容了吗?如果是这样,那么值得将其包含在您的问题中。
旁注:
依赖:
返回
UITableViewCell
是一个坏主意。您似乎将按钮的tag
分配给局部变量,但从未使用它。 (也许这是存储 UITableViewCell 索引的好地方,然后对单元进行子类化以维护按钮的 ivar?)这只是问题的一部分。The root issue is that this method:
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? (Something like[deleg.rmessages count]
?)Also, in my experience that exception often gives you more details, in particular:
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:
To return the
UITableViewCell
. You appear to assign thetag
of the button to a local variable, but never use it. (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.