如何从同一个单元格中删除UITableViewCell?

时间:2014-07-09 11:44:55

标签: ios objective-c uitableview uibutton

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

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

在db中删除行之后,我无法从单元类重新加载表。如果我尝试从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;
}

2 个答案:

答案 0 :(得分:4)

您应该在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;
}

答案 1 :(得分: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
}