【问题标题】:Selecting Multiple Table View Cells At Once in Swift在 Swift 中一次选择多个表格视图单元格
【发布时间】:2015-11-11 06:05:09
【问题描述】:

我正在尝试创建一个添加好友列表,用户可以在其中选择多个表格视图单元格,并为每个选择显示一个自定义检查。我最初使用didSelectRowAtIndexPath,但这并没有给我我正在寻找的结果,因为您可以突出显示多个单元格,但是除非您取消突出显示原始选定的行,否则您将无法再选择。然后我尝试使用didHighlighRowAtIndexPath,但这似乎不起作用,因为现在我的 indexPath 的值为零。这是我的代码:

override func tableView(tableView: UITableView, didHighlightRowAtIndexPath indexPath: NSIndexPath) {

    let indexPath = tableView.indexPathForSelectedRow

    let currentCell = tableView.cellForRowAtIndexPath(indexPath!) as! AddedYouCell

    let currentUser = PFUser.currentUser()?.username

    let username = currentCell.Username.text
    print(currentCell.Username.text)

    let Friends = PFObject(className: "Friends");

    Friends.setObject(username!, forKey: "To");
    Friends.setObject(currentUser!, forKey: "From");

    Friends.saveInBackgroundWithBlock { (success: Bool,error: NSError?) -> Void in

        print("Friend has been added.");


        currentCell.Added.image = UIImage(named: "checked.png")

    }



}

我该如何解决这个问题?谢谢

【问题讨论】:

    标签: swift pftableviewcell


    【解决方案1】:

    我不会为你编写代码,但这应该对你有所帮助:

    为了实现您的目标,您应该将数据与视图(单元格)分开。 使用一个数组(即friendList)来存储您的朋友列表和他们每个人的选定状态,并使用该数组来填充您的tableView。

    numberOfCellsForRow 等于friendList.count

    在 didSelectRowAtIndexPath 中,使用 indexPath.row 更改视图(单元格)的状态并设置数组中相同索引的状态

    在 cellForRowAtIndexpath 中,使用 indexPath.row 从数组中检索单元格的初始状态应该是什么。

    【讨论】:

    • 我不认为这对我有用,因为我使用的是 PFTableView 并且它不使用数组来填充表格视图。它使用存储在 Parse 中的信息。
    • 您能否将 Parse 对象中的数据复制到帮助器数组中,以用于我的回答中概述的目的?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多