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

Deleting Row From Custom Cell UITableView

我有一个带有自定义单元格的表格视图。我想在自定义单元格内创建一个按钮,当我单击它时,它会删除该行并使用新数组刷新主视图。当视图从存储的 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];

首先,不是通过 [[self.fullfav superview] superview]; 获得 cell 的好习惯,很有可能你最终得到的不是别的东西一个细胞。而是使用您的 tableview datasource/delegate 方法和您的数据源数组来保持您的 tableview 同步。

你的代码因为这一行而崩溃 [table deleteRowsAtIndexPaths:[NSArray arrayWithObject:cell]withRowAnimation:UITableViewRowAnimationFade];

这是因为 deleteRowsAtIndexPaths 方法接受一个 NSIndexPath 对象数组,但你传入的是一个 TableViewCellSchedule 对象数组。所以你需要传递给那个方法的数组应该是这样的 - [NSArray arrayWithObject:[table indexPathForCell:cell]]。如果您使用 deleteRowsAtIndexPaths

,也请删除 reloadData 方法

这是一个非常简单的例子。假设您有一个 UITableViewController,其原型单元格如下所示:

并且您已将标签连接到 IBOutlet,并在连接到 IBAction 的按钮上 Touch Up Inside...


Cell.h

//
//  RemoveRowTableViewCell.h
//
//  Created by Don Mag on 3/5/18.
//

#import <UIKit/UIKit.h>

@interface RemoveRowTableViewCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UILabel *theLabel;

- (void)setDidTapDeleteButtonBlock:(void (^)(id sender))didTapDeleteButtonBlock;

@end

Cell.m

//
//  RemoveRowTableViewCell.m
//
//  Created by Don Mag on 3/5/18.
//

#import "RemoveRowTableViewCell.h"

@interface  RemoveRowTableViewCell ()

@property (copy, nonatomic) void (^didTapDeleteButtonBlock)(id sender);

@end

@implementation RemoveRowTableViewCell

- (IBAction)doDidTapDeleteButton:(id)sender {
    // call back to tell the table view controller that the button was tapped
    if (self.didTapDeleteButtonBlock) {
        self.didTapDeleteButtonBlock(sender);
    }
}

@end

TableViewController.h

//
//  RemoveRowTableViewController.h
//
//  Created by Don Mag on 3/5/18.
//

#import <UIKit/UIKit.h>

@interface RemoveRowTableViewController : UITableViewController

@end

TableViewController.m

//
//  RemoveRowTableViewController.m
//
//  Created by Don Mag on 3/5/18.
//

#import "RemoveRowTableViewController.h"
#import "RemoveRowTableViewCell.h"

@interface RemoveRowTableViewController ()
@property (strong, nonatomic) NSMutableArray *theDataArray;
@end

@implementation RemoveRowTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // initialize the data array
    self.theDataArray = [@[@"A", @"B", @"C", @"D", @"E", @"F", @"G"] mutableCopy];

}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _theDataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    RemoveRowTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"removeCell" forIndexPath:indexPath];

    // Configure the cell...
    cell.theLabel.text = _theDataArray[indexPath.row];

    [cell setDidTapDeleteButtonBlock:^(id sender) {
        // button in cell was tapped, so remove that item from the data array
        [self.theDataArray removeObjectAtIndex:indexPath.row];
        // and reload the tableView
        [self.tableView reloadData];
    }];

    return cell;
}

@end

现在,当您点击单元格中的按钮时,它将 "call back" 到 tableViewController 中定义的块,其中代码将从数据数组,table 视图将被重新加载。

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

  • 首先取 table 查看添加单元格并在其上添加一个按钮。
  • 在单元格内创建按钮的 IBOutlet class。
  • 在您的视图控制器 class 中创建一个 IBAction,其中存在 table 视图数据源和委托方法。
  • 在 table 视图方法 (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];

}