【问题标题】:Deletion control button on UITableview edit mode is not workingUITableview 编辑模式上的删除控制按钮不起作用
【发布时间】:2014-11-18 11:56:52
【问题描述】:

我在单击按钮时实现了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"];
    }
}

【问题讨论】:

  • 发送删除码
  • 你去 :) @RameshMuthe
  • 你的代码没问题。你在这个控制器中使用了任何手势吗?
  • 只需在视图控制器上点击手势识别器即可关闭键盘@RameshMuthe
  • 一段时间去掉点击手势,看看有没有用?

标签: ios objective-c uitableview editmode


【解决方案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】:

    你需要实现以下方法:

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

    【讨论】:

    • 我确实实现了该方法,这就是滑动删除有效的原因。但是由于我没有在编辑模式下插入任何行,那么区分编辑样式有什么区别?
    • 对不起,我不知道这一点,因为您最初没有发布您的代码。当然,如果不想插入单元格,则不需要 if 语句。
    【解决方案3】:

    如果删除不起作用,那么这是您的表视图delegatedatasource 方法调用的问题。检查这些方法肯定应该调用

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

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

    检查您的手势识别器。

    【讨论】:

      猜你喜欢
      • 2017-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多