【问题标题】:How to delete UITableViewCell from within same cell?如何从同一单元格中删除 UITableViewCell?
【发布时间】:2014-08-30 10:15:49
【问题描述】:

我有一个带有自定义表格视图单元格的UITableView

该单元格有一个自定义删除按钮。按钮操作在单元格类中。

从数据库中删除行后,我无法从单元类重新加载表。如果我尝试从cell.superviewcell.superview.superview 获取表格视图,应用程序将崩溃。

这是单元实现

@implementation pixSavedTableViewCell

- (IBAction)deleteBusiness:(id)sender {
    NSLog(@"DELETING BUSINESS cell");
    self.hidden=YES;
    pixDBManager *dbConnection = [[pixDBManager alloc]init];
    dbConnection.businessName = self.nameLable.text;
    [dbConnection deleteBusiness];
    //[(UITableView *)self.superview.superview reloadData];
}
@end

这将从

中删除数据
-(void)deleteBusiness
{
    NSLog(@"DELETING BUSINESS");
    char *error;
    if(sqlite3_open([dbPathString UTF8String], &businessDB)==SQLITE_OK){
        NSString *deleteStatement = [NSString stringWithFormat:@"DELETE FROM SAVEDBUSINESS      WHERE 'NAME'='%@'",_businessName];
        const char *delete_stmt = [deleteStatement UTF8String];
        NSLog(@"%s",delete_stmt);
        if (sqlite3_exec(businessDB, delete_stmt, NULL, NULL, &error)==SQLITE_OK) {
            NSLog(@"Business Deleted");

        }
        else{
            NSLog(@"Cant Delete Data");
        }
        sqlite3_close(businessDB);
    }
    else{
        NSLog(@"Cant Open Data Base");
    }
}

这是我的视图控制器实现

@implementation pixSavedViewController
{
    NSMutableArray *businessArray;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    pixDBManager *dbConnection = [[pixDBManager alloc]init];
    businessArray = [[NSMutableArray alloc]init];
    [dbConnection createOrOpenDB];
    businessArray = [dbConnection getSavedBusiness];
    // Do any additional setup after loading the view.
}

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

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath      *)indexPath
{
    static NSString *customcellidentifier = @"CustomCell3";

    pixSavedTableViewCell *cell = (pixSavedTableViewCell *)[tableView   dequeueReusableCellWithIdentifier:customcellidentifier];

    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"Savedcell" owner:self options:nil];
        cell = [nib objectAtIndex:0];

    }
    pixBusinessDetails *details = [[pixBusinessDetails alloc]init];
    details = [businessArray objectAtIndex:indexPath.row];
    cell.nameLable.text = details.name;
    cell.addressLable.text = details.address;
    cell.descriptionLable.text = details.description;
    cell.scoreLable.text = details.score;
    cell.votesLable.text = details.votes;
    NSLog(@"Business Detail %@\n %@\n %@\n %@\n %@\n %@\n",details.name, details.address,details.description,details.score,details.votes,details.imageId);

    return cell;
}

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

【问题讨论】:

  • 我认为您的问题是您尝试获取超级视图的方式。您不能假设单元格超级视图是表格视图。相反,您需要遍历层次结构直到找到它。请参阅此答案以获取帮助。 stackoverflow.com/questions/15711645/…
  • 如果你有问题,我会问,而不是仅仅发布代码并告诉我们它有什么问题。
  • 你的所作所为就像杀了自己,假装死后收拾烂摊子xD

标签: ios objective-c uitableview uibutton


【解决方案1】:

与其从UITableViewCell 中删除数据,不如调用UIViewController 中的一个方法,该方法将删除数据然后重新加载表。

这样做的方法是为您的 UIViewController 创建一个协议以符合:

@protocol pixSavedTableViewCellDelegate
- (void)deleteBuisness:(NSString *) businessName
@end

@interface pixSavedTableViewCell : UITableViewCell
@property (nonatomic, weak) id<pixSavedTableViewCellDelegate> delegate;
@end

然后在UITableViewCell 中,您将删除方法更改为:

- (IBAction)deleteBusiness:(id)sender {
    [self.delegate deleteBuisness:self.nameLabel.text];
}

然后在你的UIViewController 中实现协议:

@interface pixSavedViewController <pixSavedTableViewCellDelegate>
@end

@implementation pixSavedViewController
#pragma mark - pixSavedTableViewCellDelegate
- (void)deleteBuisness:(NSString *) businessName
{
    pixDBManager *dbConnection = [[pixDBManager alloc]init];
    dbConnection.businessName = businessName;
    [dbConnection deleteBusiness];

    [self.tableView reloadData];
}

您还需要在创建单元格时设置委托:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath      *)indexPath
{
    //All your current code
    cell.delegate = self;

    return cell;
}

【讨论】:

  • 不错的方法。好一个
  • - (IBAction)deleteBusiness:(id)sender { NSLog(@"Deleting"); [self.delegate deleteBuisness:self.nameLable.text]; } @end 这个函数没有执行......
  • 你设置了代理吗?
  • 在方法中设置断点并确保 self.delegate 不为零。我想您还对 UIViewController 进行了所有更改?
  • 对不起,我很困惑,它执行到 [dbConnection deleteBusiness];但没有工作 [self.tableView reloadData];
【解决方案2】:

在我看来,这就是委托模式的用途:

让您的UITableViewController 成为您的自定义单元格的代表。

MyCustomCell *cell = ....
cell.deleteDelegate = self

//in your UITableViewController
- (void)willDeleteCustomCell:(MyCustomCell*)cell
{

}

//on your cell
- (IBAction)deleteBusiness:(id)sender
{
     [self.deleteDelegate willDeleteCustomCell:self];

     //rest of your delete code
}

【讨论】:

  • 我这样做了,但是 - (IBAction)deleteBusiness:(id)sender 这个函数没有执行...
  • 我假设此方法已在您的项目中正确连接。你做了什么让它停止工作?
  • 是的,此方法连接正确...我将 NSLog 添加到该方法中。我运行了应用程序。它没有执行 NSlog。当我删除 [self.deleteDelegate willDeleteCustomCell:self] 方法时,它执行了 NSlog..
  • 我不明白。如果您在[self.deleteDelegate willDeleteCustomCell:self] 行之后添加NSLog 并且NSLog 没有被执行,那么我认为您的应用程序必须崩溃?在方法中简单地存在一行不会阻止该方法被执行。
  • 确实发生了...我在 [self.deleteDelegate willDeleteCustomCell:self] 之前添加了 NSLog,并且方法中简单的一行确实阻止了该方法的执行
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-14
相关资源
最近更新 更多