简体   繁体   English

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

[英]Deletion control button on UITableview edit mode is not working

I have implemented UITableview edit mode when a button is clicked but every time it goes to edit mode and I click on the deletion button, nothing happens. 单击一个按钮时,我已经实现了UITableview编辑模式,但每次进入编辑模式,我点击删除按钮,都没有任何反应。 I have a view controller with a UITableview on it. 我有一个带有UITableview的视图控制器。 I have set my delegate and tableview source as well as all of my editing callbacks. 我已经设置了我的委托和tableview源以及我的所有编辑回调。 Everything is working (like reordering cells) but whenever I try to delete by pressing the delete control button, the delete button doesn't show up. 一切正常(如重新排序单元格),但每当我尝试通过按删除控制按钮删除时,删除按钮不会显示。

I am desperate since it seems like a really simple problem but no matter what I try it doesn't seem to work. 我很绝望,因为它似乎是一个非常简单的问题,但不管我尝试它似乎都不起作用。

This is how I am implementing edit mode 这就是我实现编辑模式的方式

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

This is what sets the edit mode 这是设置编辑模式的原因

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

You need to implement the following method: 您需要实现以下方法:

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

For tap gestures add the delegate and write the above code. 对于轻击手势,添加委托并编写上述代码。

If delete does not work then it is a problem of your table view delegate or datasource methods called.Check these methods should definitely be called 如果删除不起作用那么你的表视图delegatedatasource方法调用是一个问题。检查这些方法肯定应该被调用

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

You try to check if titleforDeleteConfirmationbutton is called at every time you click edit and check how many times it calls and also check if you are giving indent return value as yes in shouldIndentwhileediting method. 你尝试检查,如果titleforDeleteConfirmationbutton被称为在每次点击编辑,并检查了多少次电话,并检查,如果你是给缩进返回值作为是shouldIndentwhileediting方法。

check your gesture recognizers. 检查你的手势识别器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM