Ios 如何从同一单元格中删除UITableViewCell?
我有一个带有自定义表视图单元格的Ios 如何从同一单元格中删除UITableViewCell?,ios,objective-c,uitableview,uibutton,Ios,Objective C,Uitableview,Uibutton,我有一个带有自定义表视图单元格的UITableView 该单元格有一个自定义的删除按钮。按钮操作在cell类中 从db中删除行后,无法从cell类重新加载表。如果我试图从cell.superview或cell.superview.superview获取表视图,应用程序将崩溃 这是单元实现 @implementation pixSavedTableViewCell - (IBAction)deleteBusiness:(id)sender { NSLog(@"DELETING BUSIN
UITableView
该单元格有一个自定义的删除按钮。按钮操作在cell类中
从db中删除行后,无法从cell类重新加载表。如果我试图从cell.superview
或cell.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
@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;
}
这将从中删除数据
-(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 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
@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;
}
与其从
UITableViewCell
中删除数据,不如调用UIViewController
中的方法,该方法将删除数据,然后重新加载表
这样做的一个方法是为UIViewController创建一个协议,以符合:
@protocol pixSavedTableViewCellDelegate
- (void)deleteBuisness:(NSString *) businessName
@end
@interface pixSavedTableViewCell : UITableViewCell
@property (nonatomic, weak) id<pixSavedTableViewCellDelegate> delegate;
@end
然后在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
中删除数据,不如调用UIViewController
中的方法,该方法将删除数据,然后重新加载表
这样做的一个方法是为UIViewController创建一个协议,以符合:
@protocol pixSavedTableViewCellDelegate
- (void)deleteBuisness:(NSString *) businessName
@end
@interface pixSavedTableViewCell : UITableViewCell
@property (nonatomic, weak) id<pixSavedTableViewCellDelegate> delegate;
@end
然后在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];
}
在我看来,这就是委托模式的作用: 使您的
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
}
在我看来,这就是委托模式的作用: 使您的
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
}
我认为你的问题在于你试图获得superview的方式。不能假设单元格超级视图是tableview。相反,您需要遍历层次结构,直到找到它为止。请参阅此答案以获取帮助。如果你有问题,我会问你,而不是仅仅发布代码并告诉我们它有什么问题。你试图做的是自杀,假装在你死后收拾残局。我认为你的问题是你试图获得superview的方式。不能假设单元格超级视图是tableview。相反,您需要遍历层次结构,直到找到它为止。请参阅此答案以获取帮助。如果你有问题,我会问你,而不是仅仅发布代码并告诉我们它有什么问题。你试图做的是自杀,并假装在你死后收拾残局。好的一个-(iAction)deleteBusiness:(id)发送方{NSLog(@“Deleting”);[self.deleteBusness:self.nameLable.text];}@end此函数未执行……是否设置了委托?在方法中设置断点,并确保self.delegate不为零。我猜您也对UIViewController进行了所有更改?很抱歉,我弄错了,它执行到[dbConnection deleteBusiness];但是没有工作[self.tableView重新加载数据];好办法。好的一个-(iAction)deleteBusiness:(id)发送方{NSLog(@“Deleting”);[self.deleteBusness:self.nameLable.text];}@end此函数未执行……是否设置了委托?在方法中设置断点,并确保self.delegate不为零。我猜您也对UIViewController进行了所有更改?很抱歉,我弄错了,它执行到[dbConnection deleteBusiness];但是没有工作[self.tableView重新加载数据];我这样做了,但是-(iAction)deleteBusiness:(id)sender此函数未执行…我假设此方法已在您的项目中正确连接。你做了什么使它停止工作?是的,此方法连接正确。。。我将NSLog添加到该方法中。我运行了应用程序。它没有执行NSlog。当我删除[self.deleteDelegate willDeleteCustomCell:self]方法时,它执行了NSlog..我不明白。如果在
[self.deleteDelegate willDeleteCustomCell:self]
行之后添加NSLog
,并且NSLog
未执行,那么我假设您的应用程序一定会崩溃?简单地在方法中存在一行不会阻止该方法被执行。这确实发生了…我在[self.deleteDelegate willDeleteCustomCell:self]之前添加了NSLog,并且简单地在方法中存在一行确实阻止了该方法被执行我这样做了,但是-(iAction)deleteBusiness:(id)发件人此函数未执行…我假设此方法已在您的项目中正确连接。你做了什么使它停止工作?是的,此方法连接正确。。。我将NSLog添加到该方法中。我运行了应用程序。它没有执行NSlog。当我删除[self.deleteDelegate willDeleteCustomCell:self]方法时,它执行了NSlog..我不明白。如果在[self.deleteDelegate willDeleteCustomCell:self]
行之后添加NSLog
,并且NSLog
未执行,那么我假设您的应用程序一定会崩溃?简单地在方法中存在一行并不会阻止该方法的执行。这确实发生了……我在[self.deleteDelegate willDeleteCustomCell:self]之前添加了NSLog,而简单地在方法中存在一行确实阻止了该方法的执行