根据通过NSNotification接收的索引从tableview中删除特定行

时间:2016-05-11 11:55:34

标签: ios objective-c uitableview

我有两个视图控制器。

第一个视图控制器有一个表视图。 当用户单击第一个视图控制器的表视图中的单元格时,他将被引导到第二个视图控制器。

当用户再次返回到第一个视图控制器时,他之前点击的表格视图单元格必须从表格视图中删除

以下是我的代码

-(void)reloadOnAction:(NSNotification *)notis{


dispatch_async(dispatch_get_main_queue(), ^{

    NSDictionary *dict = notis.userInfo;
    int index_Id = [[dict objectForKey:@"post_notification"] intValue];
    if ([_statNameArray count] != 0){
        [_statNameArray removeObjectAtIndex:index_Id];
    }
    [_tblView reloadData];
});}

index_Id包含必须删除的元素。 虽然我在更改数组后重新加载了表视图,但更改并没有反映在ui中。我该如何解决这个问题?

以下是我的numberOfRowsInSection方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.statNameArray count];
}

2 个答案:

答案 0 :(得分:1)

当您返回负责第一个视图控制器的类时,请确保调用viewWillAppear。

-(void)viewWillAppear:(BOOL)animated {
     [super viewWillAppear:animated];
     [self.tableView reloadData]; // to reload selected cell
}

答案 1 :(得分:1)

您可以存储必须在Array中删除的行,并在viewWillAppear方法中删除这些行。

这将是这样的:

@interface TableViewController ()
@property (nonatomic) NSInteger selectedIndex;
@property (nonatomic, strong) NSMutableArray *statNameArray;
@end

@implementation TableViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    if ([self.statNameArray count] != 0){
        [self.statNameArray removeObjectAtIndex:self.selectedIndex];
        [self.tableView reloadData];
    }
}

- (void)reloadOnAction:(NSNotification *)notis{
    NSDictionary *dict = notis.userInfo;
    self.selectedIndex = [[dict objectForKey:@"post_notification"] intValue];
}