简体   繁体   English

在SWIFT中滚动时,我的表视图重用所选单元格

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

Good Morning 早上好

my problem is that my table view reuse the selected cell when i scroll down and up again .. i mean when i select one cell from up then scroll down, some cells which i didnt select are shown selected, also some selected cells from up are not showed selected when i scroll up again, and when that happened i am apple again to select more than just one cell which must be not allowed .. i want to mention that i tried to save the old index path from 'didSelectRowAtIndexPath'and i checked it like this in the 'CellForRowAtIndexPath'but it doesnt work : 我的问题是当我向下和向上滚动时,我的表视图重用所选单元格...我的意思是当我从上向下选择一个单元格然后向下滚动时,我选择的一些单元格被选中,也有一些选中的单元格是当我再次向上滚动时没有显示选中,当发生这种情况时我再次选择苹果不仅仅是一个必须不被允许的单元格..我想提一下,我试图从'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()
}

Now here is my code 现在这是我的代码

 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

}

You currently store the selection state in the backgroundColor. 您当前将选择状态存储在backgroundColor中。 That's not a good idea because for performance reasons cells will be reused by the tableView. 这不是一个好主意,因为出于性能原因,tableView将重用单元格。

You should save the selected indexPaths in the data model. 您应该将选定的indexPath保存在数据模型中。 If you want to allow multiple selection you can store the selected indexPaths in a NSMutableSet. 如果要允许多个选择,可以将选定的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)
}

If you only need single selection you should save the selected indexPath in an NSIndexPath? 如果您只需要单个选择,则应将选定的indexPath保存在NSIndexPath中? instance variable 实例变量

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

I had the same problem a while ago, what I did is, I added a new variable (Bool) named isset to the CustomTableViewCell, and when I created the cell for the first time, I changed it to true, so after I wanted to reuse an already set cell, I just returned the arleady set one, to not set is again: 我前一段时间遇到同样的问题,我做的是,我在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