UITableView:突出显示最后一个单元格,但其他单元格也突出显示 [英] UITableView: highlight the last cell but other cells get highlighted as well

查看:30
本文介绍了UITableView:突出显示最后一个单元格,但其他单元格也突出显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 UITableView 用作 SWRevealViewController 的一部分作为滑动菜单.

I have UITableView that I use as a sliding menu as part of SWRevealViewController.

我想选择 UITableView 中的最后一个单元格并实现以下内容:

I want to select the last cell in UITableView and implement the following:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let customCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! IGAWindAloftMenuTableViewCell

    ...


    let sectionsAmount = tableView.numberOfSections
    let rowsAmount = tableView.numberOfRowsInSection(indexPath.section)

    if (indexPath.section == sectionsAmount - 1 && indexPath.row == rowsAmount - 1)
    {
        customCell.backgroundColor = UIColor.yellowColor()
    }

    return customCell
}

当我一直向下滚动时,它起作用了——最后一个单元格被突出显示.但是,当我上下滚动时,表格中间的其他单元格也会突出显示.

When I scroll all the way down, it works -- the last cell is highlighted. However, when I scroll up and down, other cells in the middle of the table get highlighted as well.

有什么办法可以防止吗?

Is there any way to prevent it?

谢谢!

推荐答案

您必须撤消在 if 分支中对所有其他单元格所做的更改:

You have to undo the change made in the if-branch for all other cells:

if (indexPath.section == sectionsAmount - 1 && indexPath.row == rowsAmount - 1) {
    customCell.backgroundColor = UIColor.yellowColor()
} else {
    customCell.backgroundColor = UIColor.whiteColor() // or whatever color
}

产生不良副作用的原因是细胞的重复使用.创建一个单元格,然后将其用作最后一个单元格,然后移出屏幕并在其他地方重复使用.它仍然包含更改后的颜色信息,但不再位于相应位置.

The reason for the undesired side effect is the reusing of cells. A cell gets created, then it gets used as the last cell, then it moves off-screen and is reused somewhere else. It still contains the changed color information but is no longer at the corresponding position.

这篇关于UITableView:突出显示最后一个单元格,但其他单元格也突出显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆