ios - 从 UITableView 中删除自定义行

标签 ios objective-c uitableview protocols

我只是在练习 UITableView。 这是我到目前为止所做的。

  • 从头开始用 xib.file 制作细胞。
  • 数据存储在P表中。
  • 使用[MutableCopy]并将数组转换为mutableArray
  • 看起来有点奇怪,但我使用 Storyboard建立了委托(delegate)连接,所以这里没有“self.tableView.delegate = self”行。

现在的问题是我可以删除 mutableArray 中的对象,但不能删除 UI 中的对象。我选择的行保留在表中。 我知道“- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath”委托(delegate)方法有效但出了点问题。

欢迎任何建议!谢谢:)

![#import "ViewController.h"
#import "Model.h"
#import "SimpleTableCell.h"

@interface ViewController ()
{

    NSMutableArray *_recipesMC;
    NSMutableArray *_imagesMC;

    Model *_model;
}

@end

@implementation ViewController

- (void)viewDidLoad
{
    \[super viewDidLoad\];
    // Do any additional setup after loading the view, typically from a nib.

    Model *model = \[\[Model alloc\] init\];
    \[model getBack\];

    NSArray *recipes = model.recipes;
    NSArray *images = model.imagesArray;

    _recipesMC = \[recipes mutableCopy\];
    _imagesMC = \[images mutableCopy\];


}

- (void)didReceiveMemoryWarning
{
    \[super didReceiveMemoryWarning\];
    // Dispose of any resources that can be recreated.
}



# pragma mark delegat patern


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return \[_recipesMC count\];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     NSString *cellIdentifier = @"SimpleTableCell";


    SimpleTableCell *cell = (SimpleTableCell *)\[tableView dequeueReusableCellWithIdentifier:cellIdentifier\];


    if(cell == nil)
    {

        NSArray *nib = \[\[NSBundle mainBundle\] loadNibNamed:@"Empty" owner:self options:nil\];
        cell = \[nib objectAtIndex:0\];

    }

    cell.nameLabel.text = \[_recipesMC objectAtIndex:indexPath.row\];

    if (indexPath.row < \[_recipesMC count\] - 1)
    {
        UIImage *image = \[UIImage imageNamed:_imagesMC\[indexPath.row\]\];
        cell.thumbnailImageView.image = image;

    }


    return cell;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 78;

}


# pragma mark delegate touch


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%li",(long)indexPath.row);

    UIAlertView *alert = \[\[UIAlertView alloc\] initWithTitle:@"HEY" message:\[NSString stringWithFormat:@"%@",_recipesMC\[indexPath.row\]\] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil\];
    \[alert show\];

    UITableViewCell *cell = \[tableView cellForRowAtIndexPath:indexPath\];

    if (cell.accessoryType !=
        UITableViewCellAccessoryNone) {


        cell.accessoryType =
        UITableViewCellAccessoryNone;

    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }


    \[self performSegueWithIdentifier:@"CellSelectionSegue" sender:self\];

}



- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{

    NSLog(@"Clicked DELETE BUTTON at %i",indexPath.row);

    \[_recipesMC removeObjectAtIndex:indexPath.row\];
    \[_imagesMC removeObjectAtIndex:indexPath.row\];

    \[self.tableView reloadData\];

    NSLog(@"count = %i",\[_recipesMC count\]);

}



# pragma mark Segue

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    NSLog(@"Segue");
}


@end

最佳答案

我很确定你的 self.tableViewnil

这是因为即使您将自己设置为数据源和委托(delegate),您的表也会工作,但在这里您似乎已经创建了 UITableView *tableView 但没有通过您的界面链接它(如果您与 build 者一起做)。

或者您可以简单地调用 tableView 而不是 self.tableView 因为委托(delegate)协议(protocol)给您表格。

只要去做,它就会奏效。然后,你可以试试Szu的解决方案。

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{

    NSLog(@"Clicked DELETE BUTTON at %i",indexPath.row);

    [_recipesMC removeObjectAtIndex:indexPath.row];
    [_imagesMC removeObjectAtIndex:indexPath.row];

    [tableView reloadData];

    NSLog(@"count = %i",[_recipesMC count]);

}

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

相关文章:

ios - 使用唯一标识符填充结构数组

ios - 关闭 SFSafariViewController 上的半透明条

ios - 在 GoogleMapSDK 上放置标记

iphone - 普通风格的tableview : can we display only the required number of rows?

ios - 如何在 uitableview 上制作固定高度的单元格?

iphone - 无法缩进 UITableViewCell 子类

ios - iPhoneX 中安全区域的 UIStoryboard 约束问题(如何创建安全区域高度相等的 View ?)

ios - 如果没有连接,为什么调用 swiftHTTP 作为响应处理程序?

ios - 我可以打电话 - (void)touchesBegan :(NSSet *)touches withEvent:(UIEvent *)event on subview

ios - whatsapp 共享不工作(相同的代码在另一个项目中工作)