我正在尝试创建一个添加 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/