簡體   English   中英

在SWIFT中滾動時,我的表視圖重用所選單元格

[英]my table view reuse the selected cells when scroll — in SWIFT

早上好

我的問題是當我向下和向上滾動時,我的表視圖重用所選單元格...我的意思是當我從上向下選擇一個單元格然后向下滾動時,我選擇的一些單元格被選中,也有一些選中的單元格是當我再次向上滾動時沒有顯示選中,當發生這種情況時我再次選擇蘋果不僅僅是一個必須不被允許的單元格..我想提一下,我試圖從'didSelectRowAtIndexPath'和我保存舊索引路徑在'CellForRowAtIndexPath'中檢查它,但它不起作用:

if (old == indexpath)
{
        cell?.backgroundColor = UIColor.redColor()
        cell?.textLabel?.backgroundColor = UIColor.whiteColor() //to change only the thumbnail color and keep the cell color 
}

else {
        cell?.backgroundColor = UIColor.whiteColor()
}

現在這是我的代碼

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell = myTable.dequeueReusableCellWithIdentifier("WorkCell") as UITableViewCell
    cell.tag = (indexPath.row) + 1
    cell.textLabel?.text = Berufs[indexPath.row]
    var img = UIImageView(frame: CGRect(x: 10, y: 3, width: 40 , height: 40))
    cell.selectionStyle = UITableViewCellSelectionStyle.None
    img.image = UIImage(named: "transparent.png")
    cell.indentationLevel = 1
    cell.indentationWidth = 45
    cell.addSubview(img)
    return cell
}

var old = 1000

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
    let indexPath = tableView.indexPathForSelectedRow()
    let cell = tableView.cellForRowAtIndexPath(indexPath!)
    var tmpCell = view.viewWithTag(old)
    var rowNum = (cell?.tag)! - 1

    if (cell?.backgroundColor == UIColor.redColor())
    {
        cell?.backgroundColor = UIColor.whiteColor()
    }
    else if (cell?.backgroundColor != UIColor.redColor())
    {
        tmpCell?.backgroundColor = UIColor.whiteColor()
        cell?.backgroundColor = UIColor.redColor()
        cell?.textLabel?.backgroundColor = UIColor.whiteColor()

        println("du interssiert dich für \(Berufs[rowNum])")
    }

    old = rowNum + 1

}

您當前將選擇狀態存儲在backgroundColor中。 這不是一個好主意,因為出於性能原因,tableView將重用單元格。

您應該將選定的indexPath保存在數據模型中。 如果要允許多個選擇,可以將選定的indexPath存儲在NSMutableSet中。

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
    cell.selectionStyle = .None
    configure(cell, forRowAtIndexPath: indexPath)
    return cell
}

var selectedIndexPaths = NSMutableSet()

func configure(cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    cell.textLabel!.text = "Row \(indexPath.row)"
    if selectedIndexPaths.containsObject(indexPath) {
        // selected
        cell.backgroundColor = UIColor.redColor()
    }
    else {
        // not selected
        cell.backgroundColor = UIColor.whiteColor()
    }
}


override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    if selectedIndexPaths.containsObject(indexPath) {
        // deselect
        selectedIndexPaths.removeObject(indexPath)
    }
    else {
        // select
        selectedIndexPaths.addObject(indexPath)
    }
    let cell = tableView.cellForRowAtIndexPath(indexPath)!
    configure(cell, forRowAtIndexPath: indexPath)
}

如果您只需要單個選擇,則應將選定的indexPath保存在NSIndexPath中? 實例變量

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
    cell.selectionStyle = .None
    configure(cell, forRowAtIndexPath: indexPath)
    return cell
}

var selectedIndexPath: NSIndexPath?

func configure(cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    cell.textLabel!.text = "Row \(indexPath.row)"
    if selectedIndexPath == indexPath {
        // selected
        cell.backgroundColor = UIColor.redColor()
    }
    else {
        // not selected
        cell.backgroundColor = UIColor.whiteColor()
    }
}

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    if selectedIndexPath == indexPath {
        // selected same cell -> deselect all
        selectedIndexPath = nil
    }
    else {
        // select different cell
        let oldSelectedIndexPath = selectedIndexPath
        selectedIndexPath = indexPath

        // refresh old cell to clear old selection indicators
        var previousSelectedCell: UITableViewCell?
        if let previousSelectedIndexPath = oldSelectedIndexPath {
            if let previousSelectedCell = tableView.cellForRowAtIndexPath(previousSelectedIndexPath) {
                configure(previousSelectedCell, forRowAtIndexPath: previousSelectedIndexPath)
            }
        }
    }
    let selectedCell = tableView.cellForRowAtIndexPath(indexPath)!
    configure(selectedCell, forRowAtIndexPath: indexPath)
}

我前一段時間遇到同樣的問題,我做的是,我在CustomTableViewCell中添加了一個名為isset的新變量(Bool),當我第一次創建單元格時,我將其更改為true,所以在我想要之后重用已經設置的單元格,我剛剛返回arleady set one,而不是再次設置:

let cell = tableView.dequeueReusableCellWithIdentifier("Cellidentifier", forIndexPath: indexPath) as CustomTableViewCell

if cell.isset
{
        return cell
}

//set your cell here 
cell.isset = true
return cell

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM