xcode 快速更新单元附件类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26083049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 05:43:34  来源:igfitidea点击:

swift update cell accessory type

iosxcodeswift

提问by AdamXiaotCao

I am trying to update my table cell accessory type when I tap a cell. here is the code:

当我点击一个单元格时,我试图更新我的表格单元格附件类型。这是代码:

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)



}

and

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
}

Also, is there a built-in method like array.contain? Currently, I wrote a function by myself to check if an array has certain element......

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

Please help me out... I am stuck for this problem for about 8 hours

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

回答by Paulw11

Storing the reference to the cell isn't a valid strategy as cells can be re-used when the table scrolls. You can't use the current cell accessory to indicate selection state for the same reason.

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

You can use an NSIndexSetor a Swift dictionary. Here is an implementation using a dictionary -

您可以使用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
}