2

我正在使用自定义单元格,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];

}

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

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

4

1 回答 1

2

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

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

这将更新 UI,但请记住也要从数据源中删除该行。这将确保它被完全删除。

希望有帮助!

于 2013-10-24T07:26:09.330 回答