UITableview编辑模式下的删除控制按钮不起作用

时间:2014-09-24 05:35:06

标签: ios objective-c uitableview editmode

单击按钮时我已实现UITableview编辑模式,但每次进入编辑模式并单击删除按钮时,都没有任何反应。我有一个UITableview的视图控制器。我已经设置了我的委托和tableview源以及我的所有编辑回调。一切正常(如重新排序单元格),但每当我尝试通过按删除控制按钮删除时,删除按钮都不会显示。

我很绝望,因为它似乎是一个非常简单的问题,但无论我尝试它似乎都不起作用。

这就是我实现编辑模式的方式

- (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:

(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}

- (BOOL) tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [favoriteCurrencyValueList removeObjectForKey:[favoriteCurrencyList objectAtIndex:indexPath.row]];
    [favoriteCurrencyList removeObjectAtIndex:indexPath.row];
    NSUserDefaults *defaultSettings = [NSUserDefaults standardUserDefaults];
    [defaultSettings setObject:favoriteCurrencyList forKey:@"FavoriteCurrencies"];
    [defaultSettings setObject:favoriteCurrencyValueList forKey:@"PastValues"];
    [defaultSettings synchronize];
    [self.favoriteCurrencyTable beginUpdates];
    [self.favoriteCurrencyTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:(UITableViewRowAnimation)UITableViewRowAnimationLeft];
    [self.favoriteCurrencyTable endUpdates];
}

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    indexPathSelected = indexPath;
    //[self.view endEditing:YES];
}

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    [self.favoriteCurrencyList exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];

    [[NSUserDefaults standardUserDefaults] setObject:self.favoriteCurrencyList forKey:@"FavoriteCurrencies"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

这就是设置编辑模式的原因

- (IBAction)editButtonPressed:(UIBarButtonItem *)sender {
    if (self.editing && self.favoriteCurrencyTable.editing) {
        self.editing = NO;
        [self.favoriteCurrencyTable setEditing:NO animated:YES];
        [self.editButton setTitle:@"Edit"];
    }
    else {
        self.editing = YES;
        [self.favoriteCurrencyTable setEditing:YES animated:YES];
        [self.editButton setTitle:@"Done"];
    }
}

3 个答案:

答案 0 :(得分:1)

您需要实施以下方法:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}

答案 1 :(得分:1)

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if (([touch.view isKindOfClass:[UIButton class]] && touch.view.tag==<Button_TAG>)) {
        // prevent recognizing touches on the slider
        return NO;
    }
    return YES;
}

对于点击手势,添加委托并编写上述代码。

答案 2 :(得分:0)

如果删除不起作用,那么调用表视图delegatedatasource方法就会出现问题。请确保调用这些方法

1。tableView:editingStyleForRowAtIndexPath: 2。tableView:titleForDeleteConfirmationButtonForRowAtIndexPath: 3。tableView:shouldIndentWhileEditingRowAtIndexPath:

您尝试检查每次单击编辑时是否调用titleforDeleteConfirmationbutton并检查它调用的次数,并检查您是否在shouldIndentwhileediting方法中将缩进返回值设置为是。

检查你的手势识别器。

相关问题