I have a UITableView
with custom table view cell.
That cell has a custom delete button. Button action is within the cell class.
I can't reload the table from cell class after deleted row from db. If I try to get table view from cell.superview
or cell.superview.superview
, the app crashes.
This is cell implementation
@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
This will delete data from
-(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");
}
}
This is my view controller implementation
@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;
}