gpt4 book ai didi

ios - 从自定义单元格 UITableView 中删除行

转载 作者:行者123 更新时间:2023-11-29 00:02:22 27 4
gpt4 key购买 nike

我有一个带有自定义单元格的表格 View 。我想在自定义单元格内创建一个按钮,当我单击它时,它会删除该行并使用新数组刷新主视图。当 View 从存储的 userDefaults 加载时获取数组,如下所示。如果我注释掉 delete rows 行,下面的代码可以工作,但它将使用旧数组信息刷新并且不会获取新数组。我已经尝试了所有方法,但当前代码出现以下错误:

-[TableViewCellSchedule section]: unrecognized selector sent to instance 0x7ffd65092800

任何想法表示赞赏。

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:favid forKey:@"favid"];
[userDefaults synchronize];
TableViewCellSchedule *cell = (TableViewCellSchedule *)[[self.fullfav superview] superview];
UITableView *table = cell.superview;
[table reloadData];
[table deleteRowsAtIndexPaths:[NSArray arrayWithObject:cell]
withRowAnimation:UITableViewRowAnimationFade];

最佳答案

我使用这种方法删除单元格 --

  • 首先在表格 View 中添加单元格并在其上添加一个按钮。
  • 在单元格类中创建按钮的 IBOutlet。
  • 在 View Controller 类中创建一个 IBAction,其中存在 TableView 数据源和委托(delegate)方法。
  • 内部 tableview 方法 (cellForRowAtIndexPath) 为此按钮提供标签值。即行号
  • 当单击单元格按钮时,您将在按钮的 IBAction 中获取其标签值。
  • 然后您可以通过从数组中删除该索引来删除行。
  • 然后重新加载数据。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
ContactTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
// your initialization -----------
cell.btnFriend.tag = indexPath.row;
return cell;
}

// this button is present on cell. By tag value of button we can get cell index which is selected, because tag value and array index is same
- (IBAction)tapOnUnfriend:(id)sender
{
UIButton *btn = (UIButton *) sender;
NSString *strSelectedTitle = [arrMyFriendList objectAtIndex :btn.tag];
[arrMyFriendList removeObjectAtIndex:btn.tag];
[self.tableViewContactList reloadData];

// or if you want cell of tableview you can get by tag value
// NSIndexPath *indexPath = [NSIndexPath indexPathForRow:btn.tag inSection:0];
// YourCell *cell = (YourCell *)[tblRegister cellForRowAtIndexPath:indexPath];

}

关于ios - 从自定义单元格 UITableView 中删除行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49115294/

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