質問

I am using Custom Cell for UITableView and there is a UIButton on it which should remove the current cell from the table view. In cellForRowAtIndexPath I am doing

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

And in Function for removing

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

}

Now problem is that it is showing animation on the correct cell but I am not able to remove the custom cell from the table view. And whenever I load the view again i.e. comes from other screen it does not shows the cell on which I have clicked for removing.

Any help will be appreciated...

役に立ちましたか?

解決

To delete a row/cell from table view, you need to call the deleteRowsAtIndexPaths:withRowAnimation: method on your UITableView instance.

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

This will update the UI, but remember to remove that row from your data source as well. This will ensure it is deleted completely.

Hope that helps!

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top