2013-02-27 5 views
0

Я столкнулся с небольшой проблемой. У меня есть UIButton в моей ячейке, в которой при нажатии я хочу, чтобы ячейка была удалена. Я попытался это сделать, но дал ошибку.Удалить ячейку из UITableViewCell?

- (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]; 
} 

У меня такое чувство, что я не правильно указываю indexPath, не знаю как.

Любая помощь приветствуется!

+0

что сообщение об ошибке? –

+0

Каково значение для indexPath? вы можете проверить, является ли indexPath действительным, а затем удалить строку в противном случае. «Неверный выбор» –

+0

Не на данный момент компьютер, а что-то про объявление для UIButton, что заставляет меня поверить в его индекс indexPath. – ranjha

ответ

1

Я бы так сделал. У меня есть свой класс 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 
} 

-

// .h 

@interface MyView : UIView <UITableViewDataSource, UITableViewDelegate, MyCustomCellDelegate> { 


NSInteger rowOfTheCell; 

ПРИМЕЧАНИЕ: Не забудьте установить делегата MyCustomCellDelegate в камеру.

// .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]; 
} 
0

параметр прохождения как целое.

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

Это была проблема для него. Он не смог получить правильный indexPath. Затем, как он передаст целочисленное значение вашему методу. –