gpt4 book ai didi

ios - 快速更新电池附件类型

转载 作者:可可西里 更新时间:2023-11-01 00:38:10 25 4
gpt4 key购买 nike

我正在尝试在点击一个单元格时更新我的​​表格单元格配件类型。这是代码:

var selectedCellArray :[FriendTableViewCell] = []
var friends: [PFUser] = []



override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var selectedCell = self.tableView(tableView, cellForRowAtIndexPath: indexPath) as FriendTableViewCell

if (selectedCell.accessoryType == UITableViewCellAccessoryType.None){

selectedCell.accessoryType = .Checkmark
selectedCellArray.append(selectedCell)
}else if (selectedCell.accessoryType == UITableViewCellAccessoryType.Checkmark){

selectedCell.accessoryType = .None
var index = 0
for cell in selectedCellArray{
if (cell != selectedCell){
index++
}else{
break
}
}

selectedCellArray.removeAtIndex(index)


}
self.tableView(tableView, cellForRowAtIndexPath: indexPath)



}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("friend") as? FriendTableViewCell ?? FriendTableViewCell()
var round = 0
var friend: AnyObject = self.friends[indexPath.row]


cell.firstNameLabel.text = friend["firstName"] as? String

cell.lastNameLabel.text = friend["lastName"] as? String
println(selectedCellArray)
round++
var hasFound = false
if (self.checkSelectedArray(selectedCellArray, target: cell)){
cell.accessoryType = .Checkmark
}else{
cell.accessoryType = .None
}

cell.firstNameLabel.sizeToFit()
cell.lastNameLabel.sizeToFit()
return cell
}
func checkSelectedArray(selectedArray:[FriendTableViewCell], target:FriendTableViewCell) -> Bool{
for cell in selectedCellArray{
if cell.isEqual(target){
return true
}
}
return false
}

还有,有没有像array.contain这样的内置方法?目前,我自己写了一个函数来检查一个数组是否有某个元素......

请帮帮我……我被这个问题困了大约 8 个小时

最佳答案

存储对单元格的引用不是一个有效的策略,因为当表格滚动时单元格可以被重新使用。出于同样的原因,您不能使用当前单元格附件来指示选择状态。

您可以使用 NSIndexSet 或 Swift 字典。这是一个使用字典的实现 -

var selectedCells :Dictionary<String,PFUser>()

var friends: [PFUser] = []

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

let selectedCell = tableView.cellForRowAtIndexPath(indexPath) as FriendTableViewCell

let objectId=friends[indexPath.row].objectId

if (selectedCells[objectId] != nil){
selectedCell.accessoryType = .None
selectedCells.removeValueForKey(objectId)
} else
selectedCell.accessoryType = .Checkmark
selectedCells[objectId]=friends[indexPath.row]
}

tableView.deselectRowAtIndexPath(indexPath, animated:false)

}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("friend") as? FriendTableViewCell ?? FriendTableViewCell()
var round = 0
var friend: AnyObject = self.friends[indexPath.row]

cell.firstNameLabel.text = friend["firstName"] as? String

cell.lastNameLabel.text = friend["lastName"] as? String

if (selectedCells[friend.objectId] != nil){
selectedCell.accessoryType = .Checkmark
} else
selectedCell.accessoryType = .None
}

cell.firstNameLabel.sizeToFit()
cell.lastNameLabel.sizeToFit()
return cell
}

关于ios - 快速更新电池附件类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26083049/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com