如果返回空数组,UITableView editActionsForRowAtIndexPath将停止工作

Kei*_*ley 12 action uitableview ios

我有一个UITableView,我添加了一些自定义滑出按钮.这些都按预期工作.然而,在我的表中有一个场景,其中一行可以处于没有滑出按钮相关的状态,因此我返回了一个空的动作数组,因此滑出按钮不会出现.不幸的是,一旦发生这种情况,UITableView就会停止调用我的editActionsForRowAtIndexPath,有效地禁用我表中所有行的滑出按钮......在应用程序重新启动之前,它似乎是永久性的.

这是预期的行为吗?

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]?
{
    if mydata[indexPath.row].EditAvailable()
    {
        var editAction = UITableViewRowAction(style: .Default, title: "Edit", handler: editHandler)
        return [editAction]
    }
    else
    { 
        return []
    }
}
Run Code Online (Sandbox Code Playgroud)

fst*_*tef 23

我解决这个问题的方法是实现这个功能,

func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool
Run Code Online (Sandbox Code Playgroud)

在这里,如果您不希望预期的行具有滑动功能,则应该返回false.

所以你的代码看起来像这样

func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
  return mydata[indexPath.row].EditAvailable()
}

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]?
{
  var editAction = UITableViewRowAction(style: .Default, title: "Edit", handler: editHandler)
  return [editAction]
}
Run Code Online (Sandbox Code Playgroud)

然后仅针对您指示的可编辑的那些调用editActionsForRowAtIndexPath.


Dai*_*ima 7

请尝试使用tableView:editingStyleForRowAtIndexPath:.
您可以控制幻灯片本身的开/关.

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (/* mydata[indexPath.row].EditAvailable() */) {
        return UITableViewCellEditingStyleDelete;
    } else {
        return UITableViewCellEditingStyleNone;
    }
}
Run Code Online (Sandbox Code Playgroud)

(我在Objective-c中测试过)


D_D*_*D_D 6

避免传递空数组 -

tableView editActionsForRowAtIndexPath

最好办理入住手续 -

tableView canEditRowAtIndexPath

像这样格式化你的代码:

func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        if mydata[indexPath.row].EditAvailable()  {
            return true
        }
        return false
    }

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]?
    {
        var editAction = UITableViewRowAction(style: .Default, title: "Edit", handler: editHandler)
        return [editAction]
    }
Run Code Online (Sandbox Code Playgroud)


Dav*_*son 0

不久前,我使用 ob-c (尚未在 swift 中测试)对某些 tableCell 实现了类似的选择性操作,但将选择性放在了 handleSwipe 中,下面我对除最后一个之外的所有操作都有一个操作:

- (void)handleSwipeLeft:(UISwipeGestureRecognizer *)gestureRecognizer{
    CGPoint location = [gestureRecognizer locationInView:self.tableView];
    NSIndexPath *swipedIndexPath = [self.tableView indexPathForRowAtPoint:location];
    if(!(swipedIndexPath.row<[coreDataHelper getAllTimerLists].count) ){
        [self.tableView setEditing:!self.tableView.editing animated:YES];
    }
}
Run Code Online (Sandbox Code Playgroud)

这对您有帮助吗?或者您是否需要多个不同的操作数组,而不是只有一个数组并停用/激活它?