簡體   English   中英

從自定義單元格UITableView刪除行

[英]Deleting Row From Custom Cell UITableView

我有一個帶有自定義單元格的tableview。 我希望在自定義單元格中創建一個按鈕,當我單擊該按鈕時,它將刪除該行並使用新數組刷新主視圖。 當從userDefaults加載視圖時,將獲取數組,如下所示。 如果我注釋掉刪除行行,則以下代碼有效,但它將使用舊的數組信息刷新,而不會獲取新的數組。 我已經嘗試了一切,當前代碼給了我以下錯誤:

-[TableViewCellSchedule部分]:無法識別的選擇器已發送到實例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];

我使用這種方法刪除單元格-

  • 首先以表格視圖添加單元格並在此添加按鈕。
  • 在單元格類中創建按鈕的IBOutlet。
  • 在存在表視圖數據源和委托方法的視圖控制器類內創建IBAction。
  • 內部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]; } 

首先,通過執行[[self.fullfav superview] superview];來獲得您的cell 不是一個好習慣[[self.fullfav superview] superview]; 您最終可能會得到除細胞以外的其他東西。 而是使用tableview數據源/委托方法和數據源數組來保持tableview同步。

由於此行,您的代碼崩潰了[table deleteRowsAtIndexPaths:[NSArray arrayWithObject:cell]withRowAnimation:UITableViewRowAnimationFade];

這是因為deleteRowsAtIndexPaths方法接受一個NSIndexPath對象數組,但是您要傳遞一個TableViewCellSchedule對象數組。 因此,您需要傳遞給該方法的數組應該像這樣- [NSArray arrayWithObject:[table indexPathForCell:cell]] 如果您使用的是deleteRowsAtIndexPaths也請刪除reloadData方法

這是一個非常簡單的例子。 假設您有一個UITableViewController ,其原型單元格如下所示:

在此處輸入圖片說明

並且您已將標簽連接到IBOutlet,並在連接到IBAction的按鈕上觸摸了內部...


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

現在,當您單擊單元格中的按鈕時,它將“回調”到tableViewController中定義的塊,其中代碼將從數據數組中刪除該行的元素,並重新加載表視圖。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM