• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 在条件和 UI 澄清的情况下删除 tableView 行

[复制链接]
菜鸟教程小白 发表于 2022-12-12 15:05:41 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我需要一种允许用户删除 tableView 行的方法,前提是满足条件(如果 source == "MyApp")。我在下面提供了一个有效的示例。

func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    return true
}

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    let source = objectSample[indexPath.row].source.name

    if source == "MyApp"{
        if (editingStyle == UITableViewCellEditingStyle.Delete) {
            let UUIDtoDelete = objectSample[indexPath.row].UUID
            deleteUUIDobject(UUIDtoDelete)
            objectSample.removeAtIndex(indexPath.row)
            tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)

            println("Deleted")
        }
    }
    else {
        println("Not allowed to delete")
    }
}

生成的 UI 对用户来说有点困惑,因为无论他/她是否可以实际删除该行,它都会为所有行留下一个红色的滑动删除按钮。因此,如果不满足条件,我尝试将删除按钮设为灰色:

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {
    let source = objectSample[indexPath.row].source.name
    println(source)

    if source == "MyApp"{
        var deleteButtonRed = UITableViewRowAction(style: .Default, title: "Delete", handler: { (action, indexPath) in
            println("To delete is OK, set color red")
        })
        deleteButtonRed.backgroundColor = UIColor.redColor()
        return [deleteButtonRed]
    }

    else{
        var deleteButtonGray = UITableViewRowAction(style: .Default, title: "Delete", handler: { (action, indexPath) in
            println("To delete is NOT OK, ret color gray")
        })
        deleteButtonGray.backgroundColor = UIColor.grayColor()
        return [deleteButtonGray]

        }

    return ["Will Never Happen"]
}

我的问题是这些方法不能一起使用。出于某种原因,如果我应用方法 editActionsForRowAtIndexPath,则永远不会评估 commitEditingStyle。即使 source == "MyApp"也不会删除该行。如何将两者结合在一起?
或者有没有更好的方法来显示用户可以/不能删除哪些 tableView 条目?

任何帮助将不胜感激!谢谢。



Best Answer-推荐答案


试试这个以防止其他单元格编辑:

func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    return source == "MyApp"
}

或者这样可以让其他单元格可编辑,但不能删除控制:

func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
    if source == "MyApp" {
        return UITableViewCellEditingStyle.Delete
    } else {
        return UITableViewCellEditingStyle.None
    }
}

关于ios - 在条件和 UI 澄清的情况下删除 tableView 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29708861/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap