【问题标题】:swipe not showing delete button滑动不显示删除按钮
【发布时间】:2015-01-12 12:45:46
【问题描述】:

真的很奇怪。 :(

我正在尝试在 tableview 中实现滑动删除。因为这就是我所拥有的。

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

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"commitEditingStyle===%@", editingStyle);
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //add code here for when you hit delete
        NSLog(@"now delete this cell");
    }
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}

当我滑动时,滑动完成,但我看不到删除按钮。

知道发生了什么吗?


编辑 1

现在更奇怪了。

当我在viewDidLoad 中说mainTableView.editing = YES; 时,我有以下内容。

为什么左侧出现删除选项?

还有编辑选项,它仍然和第一张图片一样。


编辑 2

// table view delegates
- (int)numberOfSectionsInTableView:(UITableView *) tableView {
    return 1;
}

- (int) tableView:(UITableView *) tableView numberOfRowsInSection:(NSInteger)section {
    return actualProductsArray.count;
}

-(UITableViewCell *) tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"];
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MainCell"];

    // created label and added into cell...

    cell.backgroundColor = [UIColor clearColor];
    cell.contentView.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;


    return cell;

}

【问题讨论】:

  • 也许这个答案可能会有所帮助:stackoverflow.com/q/19645356/2274694
  • @LyndseyScott:看我的更新......有些奇怪......
  • mainTableView.editing = YES 不会解决问题。
  • @Harry :我添加了该选项只是为了看看根据以下答案会发生什么......
  • @Harry:知道为什么会这样吗?

标签: ios objective-c uitableview swipe


【解决方案1】:

虽然这不是真正的答案,但下面是错误的。

在定义 tableview 的大小时,我将其宽度定义为 1080 对 320,因此我无法看到删除按钮,因为它位于屏幕前面。

【讨论】:

  • 我不认为这是唯一的问题。当您执行此 mainTableView.editing = YES 时,内容应移至右侧。但在屏幕截图中,内容位于按钮下方。
【解决方案2】:

我遇到了同样的问题,它与表格的宽度或没有 canEditRowAtIndexPath 无关。我注意到当我向左滑动时,右侧的导航栏按钮(编辑)会闪烁。我在代码中发现的错误是我在 setEditing 方法中调用了 tableView.reloadData()。

override func setEditing(editing: Bool, animated: Bool) {
     super.setEditing(editing, animated: animated)
    tableView.reloadData()
}

当我删除 tableView.reloadData() 行时,Delete 按钮按预期显示。我执行 reloadData 的原因是因为在编辑模式下,我在表格底部添加了一个 Insert 行,其中包含 editingStyleForRowAtIndexPath = .Insert

【讨论】:

    【解决方案3】:

    你必须在 ViewDidload 中尝试 Tableview.editing=YES

    【讨论】:

      【解决方案4】:

      我只使用以下行在我的代码中滑动删除它对我来说非常好。

      - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
      {
          if (editingStyle == UITableViewCellEditingStyleDelete)
          {
              // Delete the row from the data source
              //[self.tblinboxmsg deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
          }
          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)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { 是激活滑动删除所必需的。您所显示的内容将识别在滑动选项中单击的内容...
      • 其实可以忽略canEditRowAtIndexPath的实现。如果没有实现,默认为YES
      【解决方案5】:

      我认为您直接在单元格上而不是在 cell.contentView 上添加标签。

      如果您在此处提供代码

      // 创建标签并添加到单元格中...

      我们可以为您提供更多帮助。

      tableView.editing = true 
      

      具有您在图像编辑 1 上的行为。(按钮位于左侧) 但是按钮在内容上,不应该。因为您是直接在单元格上添加标签

      请在 contentView 中添加标签,然后重试

      【讨论】:

      • 不,问题是 tableview 的宽度比屏幕的大小要大...就是这样...
      【解决方案6】:

      我知道这个问题已经解决了一年多了,但是对于可能遇到这个问题的一些人来说,这可能只是一个约束问题。这就是我解决我的问题的方法。

      不敢相信我已经尝试解决这个问题好几天了,真的!因为我认为它在我的代码实现中的某个地方,因为我从 UITableViewController 转换到 UIViewController 上的 UITableView。我刚刚从原始的UITableViewController 复制了UITableViewCell,并且代码在原始 UITableViewController 实现中运行良好。

      @Fahim Parkar 第一个屏幕截图与我的场景几乎相同。滑动而不显示删除。我认为这意味着canEditRowAtIndexPath 已经实现。我相信他的答案是在代码中设置的,但它让我尝试检查我的约束并最终修复它

      【讨论】:

        猜你喜欢
        • 2011-03-19
        • 2014-01-14
        • 1970-01-01
        • 2014-05-31
        • 2013-03-15
        • 1970-01-01
        • 2017-04-05
        • 1970-01-01
        • 2010-10-03
        相关资源
        最近更新 更多