swift - 在 Swift 中一次选择多个 TableView 单元格

标签 swift pftableviewcell

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

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")

    }



}

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

最佳答案

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

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

numberOfCellsForRow 等于friendList.count

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

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

关于swift - 在 Swift 中一次选择多个 TableView 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33645096/

相关文章:

ios - 允许用户从下拉列表或 map 中选择位置?

ios - 如何使用 "Outlets cannot be connected to repeating content"解决此错误?

ios - 表格 View 单元格大小更改

ios - PFQueryTableView 自定义单元格分隔符不显示

ios - 使用 Swift 创建滑动动画

ios - 如何在真实设备上将渐变应用到 iOS 10 中的 UIView

swift - 如何显示用户在提要上发布消息的时间?

ios - TableView 单元格选择错误

ios - 即使在展开它之后也可以选择具有值(value)

快速的字符串分离,但包括