UITableViewCell 不会在 swift 中保持突出显示

Posted

技术标签:

【中文标题】UITableViewCell 不会在 swift 中保持突出显示【英文标题】:UITableViewCell does not remain highlighted in swift 【发布时间】:2016-09-07 10:18:12 【问题描述】:

我在侧边菜单/导航抽屉中为我的表格视图创建了一个自定义单元格,并且在大多数情况下一切似乎都工作正常,但是当我选择其中一行时(这会将我带到另一个 ViewController),然后来通过导航抽屉从后续表格视图返回,单元格仍然被取消选择。 例如:当我从侧面菜单表视图中选择 A 时,我被带到 AViewController,当我选择 B 时,我被带到 BViewController。所以基本上当我选择 A 并返回侧面菜单时,A 应该保持突出显示。我怎样才能做到这一点。 代码:

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

    tableView.deselectRowAtIndexPath(indexPath, animated: true)
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! slideMenuIconTableViewCell

    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

    switch indexPath.row 
    case 0:
        let viewController:UIViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("MyCartViewController")
        sideMenuViewController?.contentViewController = UINavigationController(rootViewController: viewController)
        sideMenuViewController?.hideMenuViewController()
        break
    case 1:
        let nextViewController:ProductListingViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("ProductListingViewController") as! ProductListingViewController
        nextViewController.productID = "1"
        nextViewController.navigationTitle = "Tables"

        sideMenuViewController?.contentViewController = UINavigationController(rootViewController: nextViewController)
        sideMenuViewController?.hideMenuViewController()
        break
    case 2:
        let nextViewController:ProductListingViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("ProductListingViewController") as! ProductListingViewController
        nextViewController.productID = "3"
        nextViewController.navigationTitle = "Sofas"

        sideMenuViewController?.contentViewController = UINavigationController(rootViewController: nextViewController)
        sideMenuViewController?.hideMenuViewController()
        break

    default:
        break
    

有人可以帮忙吗?

【问题讨论】:

首先,删除break 行(default 后面的break 除外),在 Swift 中不需要在 case 末尾添加 break。其次,您明确告诉表格视图取消选择tableView.deselectRowAtIndexPath... 行中的单元格。第三:appDelegate 是干什么用的?代码中没有用到。 PS:cell 也没有使用。无论如何,您不得在 cellForRowAtIndexPath 之外创建重复使用的单元格。 @vadian 谢谢,我会按照您的建议进行更改。但是我用什么代替 tableView.deselectRowAtIndexPath 如果要保持选中的单元格,只需删除该行。 因为无法检查问题的整个代码。我猜是,您可能正在分配表格,或者您可能正在重新加载表格,可能在 viewWillAppear 中。您可以自行检查,如果这是您的问题,请告诉我们 【参考方案1】:

您不应重新加载或分配表以保持该行的状态为选中状态。当您在 viewWillAppear 中重新加载表格时,它会重置选择,从而导致您的表格不显示先前的选择。由于您的要求是在viewWillAppear 中重新加载它,那么解决方案将是

1 - 保留一个全局变量以保持选中 object。 (保留索引可能是错误的,因为您的列表内容是动态的)

2 - 在重新加载之前过滤掉之前在全局对象中分配的对象。

3 - 找出数组中对象的索引。

4 - 然后使用cellForRowAtIndex 中的条件语句使特定行突出显示。

希望对你有帮助

编码愉快...

【讨论】:

如果能提供您的解释代码sn-p,我将不胜感激【参考方案2】:

伙计们,我找到了解决方案。我从 Janmenjaya 上面的建议 (https://***.com/a/39382360/6077720) 中得到了帮助,并编写了以下有效的代码:

var cellToDeSelect:UITableViewCell?
var isHighlighted: Bool?


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

    cellToDeSelect = tableView.cellForRowAtIndexPath(indexPath)!
    cellToDeSelect?.contentView.backgroundColor = UIColor.grayColor()
    isHighlighted = true


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 

    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! slideMenuIconTableViewCell
    cell.layoutMargins = UIEdgeInsetsZero
    cell.contentView.backgroundColor = UIColor.clearColor()
    if (isHighlighted == true)
        cellToDeSelect?.contentView.backgroundColor = UIColor.grayColor()

    

    return cell

【讨论】:

【参考方案3】:

您可以向类 slideMenuIconTableViewCell 'isHighlighted' 添加一个布尔值,并在选中时将其设置为 true。返回 tableview 后,检查布尔值并为单元格设置其他背景颜色。

【讨论】:

我有点迷茫,能否提供代码

以上是关于UITableViewCell 不会在 swift 中保持突出显示的主要内容,如果未能解决你的问题,请参考以下文章

在 Swift 中设置动画 UITableViewCell 突出显示颜色

图像在 UITableViewCell ios Swift 4 中没有得到适当的高度

当我尝试以编程方式添加 UIView 时,UITableViewCell 高度不会自动调整(而是压扁我的 UIView)

如果在之前在 UITextField 上键入之前未“加载”,则 UITableViewCell 中的 UITextView 不会显示

Swift UIButton背景颜色不会改变

swift:如何在单击按钮上对所有单元格进行 TTS