Ios 是否从UITableViewCell中删除单元格?

Ios 是否从UITableViewCell中删除单元格?,ios,iphone,uitableview,cell,Ios,Iphone,Uitableview,Cell,我面临一个小问题。我的手机上有一个UIButton,按下该按钮时,我希望手机被删除。我尝试过这个,但给出了错误 - (IBAction)deleteCell:(NSIndexPath *)indexPath { MainViewController *view = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil]; [view.nameArray removeObjectAtIndex:in

我面临一个小问题。我的手机上有一个UIButton,按下该按钮时,我希望手机被删除。我尝试过这个,但给出了错误

- (IBAction)deleteCell:(NSIndexPath *)indexPath {
MainViewController *view = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];
[view.nameArray removeObjectAtIndex:indexPath.row];
[view.priceArray removeObjectAtIndex:indexPath.row];
[view.mainTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
}
我有一种感觉,我没有正确地指定indexath,不知道如何指定


感谢您的帮助

我会这样做的。我有自己的MyCustomCell类,其中每个单元格都有按钮

//MyCustomCell.h

@protocol MyCustomCellDelegate <NSObject>

-(void)deleteRecord:(UITableViewCell *)forSelectedCell;

@end

@interface MyCustomCell : UITableViewCell {

}
@property (nonatomic, strong) UIButton *deleteButton;
@property (unsafe_unretained) id<MyCustomCellDelegate> delegate;
@end

//MyCustomCell.m

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
 {
    self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
    if (self) {
     self.deleteButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 150, 44)];
    [self.deleteButton setTitle:@"Delete your record" forState:UIControlStateNormal];
    [self.deleteButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    [self.deleteButton addTarget:self action:@selector(editRecord:) forControlEvents:UIControlEventTouchUpInside];
    }
 }

-(IBAction)editRecord:(id)sender {
    [self.delegate deleteRecord:self]; // Need to implement whoever is adopting this protocol
}

以整数形式传递参数

- (IBAction)deleteCell:(NSInteger)indexPath {
MainViewController *view = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];
[view.nameArray removeObjectAtIndex:indexPath];
[view.priceArray removeObjectAtIndex:indexPath];
[view.mainTable reloadData];
}

错误消息是什么?indexPath的值是什么?你能不能测试一下indexPath是否有效,然后删除行,否则会提醒“错误选择”目前不是由计算机进行的,而是关于为UIButton声明的内容,这让我相信它与indexPath有关。确切地说,我不知道如何从单元格中声明单元格的indexPath。这是他的问题。他没能得到正确的答案。然后他将如何将整数值传递给您的方法。
// .m

-(void)deleteRecord:(UITableViewCell*)cellSelected
{
     MyCustomCell *selectedCell = (MyCustomCell*)cellSelected;
     UITableView* table = (UITableView *)[selectedCell superview];
     NSIndexPath* pathOfTheCell = [table indexPathForCell:selectedCell]; //current indexPath
     rowOfTheCell = [pathOfTheCell row];  // current selection row

     [view.nameArray removeObjectAtIndex:rowOfTheCell];
     [view.priceArray removeObjectAtIndex:rowOfTheCell];
     [view.mainTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:pathOfTheCell]    withRowAnimation:UITableViewRowAnimationRight];
}
- (IBAction)deleteCell:(NSInteger)indexPath {
MainViewController *view = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];
[view.nameArray removeObjectAtIndex:indexPath];
[view.priceArray removeObjectAtIndex:indexPath];
[view.mainTable reloadData];
}