无法从UITableView中删除自定义单元格

时间:2013-10-24 07:08:23

标签: ios iphone objective-c uitableview custom-cell

我正在使用UITableView的自定义单元格,其上有一个UIButton,它应该从表格视图中删除当前单元格。 在cellForRowAtIndexPath我在做

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"CellIdentifier";
    NSLog(@"Creating Cell");

    FADynamicCell *cell= (FADynamicCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil)
    {
        cell = [[[NSBundle mainBundle]loadNibNamed:NSStringFromClass([FADynamicCell class])
                                             owner:nil
                                           options:nil] lastObject];
    }

    currentIndexPath = indexPath;

    UIButton *removeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    removeButton.tag = indexPath.row +100;
    removeButton.frame =  cell.removeButton.frame;
    removeButton.backgroundColor = [UIColor colorWithRed:255./255 green:65./255 blue: 21./255 alpha:1];
    removeButton.titleLabel.font = [UIFont systemFontOfSize:12];
    removeButton.titleLabel.textAlignment = NSTextAlignmentCenter;
    [removeButton setTitle:@"Remove" forState:UIControlStateNormal];
    [removeButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [removeButton addTarget:self action:@selector(removing) forControlEvents:UIControlEventTouchUpInside];
    [cell addSubview:removeButton];

    return cell;
}

在功能中删除

-(void) removing
{
    [TOperations deleteTickerFromGroup:tickerGroupID andTickerSymbol:selectedSymbol];

    NSNumber *selectedRowIndex1 = [NSNumber numberWithInteger:currentIndexPath.row];
    [tableView1 beginUpdates];
    NSIndexPath *previousPath = [NSIndexPath indexPathForRow:selectedRowIndex1.integerValue-1 inSection:0];
    [tableView1 endUpdates];
    [tableView1 reloadData];

}

现在问题是它在正确的单元格上显示动画但是我无法从表格视图中删除自定义单元格。每当我再次加载视图,即来自其他屏幕时,它不会显示我单击要删除的单元格。

任何帮助将不胜感激......

1 个答案:

答案 0 :(得分:2)

要从表格视图中删除行/单元格,您需要在deleteRowsAtIndexPaths:withRowAnimation:实例上调用UITableView方法。

NSIndexPath *previousPath = [NSIndexPath indexPathForRow:selectedRowIndex1.integerValue-1 inSection:0];
[self.yourTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:previousPath]  withRowAnimation:UITableViewRowAnimationFade];

这将更新用户界面,但请记住也要从数据源中删除该行。这将确保完全删除。

希望有所帮助!