【问题标题】:Highlight tableview cell when tapped and unhighlight when alert controller action buttons are pressed点击时突出显示 tableview 单元格,按下警报控制器操作按钮时取消突出显示
【发布时间】:2016-10-08 23:01:01
【问题描述】:

当用户触摸blue 中的单元格时,我喜欢突出显示tableview 单元格中的行,然后当用户选择操作按钮时,我希望将其更改为gray 颜色

   func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    var newCell = tableView.cellForRow(at: indexPath)!
    newCell.backgroundColor = UIColor.blue

    let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)

    let okButton = UIAlertAction(title: "Ok", style: .default, handler: { (action) -> Void in
        print("Ok button tapped")
        var newCell = tableView.cellForRow(at: indexPath)!
        newCell.backgroundColor = UIColor.gray
    })

    let cancelButton = UIAlertAction(title: "Cancel", style: .cancel, handler: { (action) -> Void in
        print("Cancel button tapped")
        var newCell = tableView.cellForRow(at: indexPath)!
        newCell.backgroundColor = UIColor.gray
    })

    alertController.addAction(okButton)
    alertController.addAction(cancelButton)

    present(alertController, animated: true, completion: nil)
}

func tableView(_ tableView: UITableView, didhighlightRowAt indexPath: IndexPath) {
    var newCell = tableView.cellForRow(at: indexPath)!
    newCell.backgroundColor = UIColor.clear
}

我这样做了,它以white 的颜色突出显示单元格,点击操作按钮后,它仍然保持white,但是当我点击另一个单元格时,前一个单元格转动并保持gray

【问题讨论】:

    标签: ios swift uitableview uiactionsheet


    【解决方案1】:

    在视图控制器初始化中:

    NSMutableSet *selectedRows = NSMutableSet.alloc.init;
    NSIndexPath *lastSelected = nil;
    

    在表视图初始化中: tableview.allowsMultipleSelection = false

    在选择行中: 火灾警报(你已经有了),lastSelected = indexPath;[selectedRows addObject:indexPath.row]

    在行的单元格中: cell.selectionStyle = UITableViewSelectionStyleBlue;

    if ([selectionSet contains:indexPath.row])
        cell.backgroundColor = UIColor.grayColor;
    else
        cell.backgroundColor = UIColor.clearColor;
    

    在警报视图委托中: [tableview deselectRowAtIndexPath:selectedIndexPath][tableview reloadIndexPath:lastSelection]

    这应该: 1)从清除所有单元格开始

    2) 单击时将单元格变为蓝色,默认突出显示

    3) 选择警报视图后重绘单元格

    4) 如果单元格在 selectedRow 集中,它将用灰色重绘

    【讨论】:

    • 1.我的单元格是清晰的颜色 2. 当用户点击时它们变成蓝色 3. 当用户按下操作按钮时它们变成灰色并保持灰色。我希望你现在清楚了。很抱歉造成混乱
    • 我现在明白了 :) 我最近的编辑应该可以解决问题。关键是引用 cellForRow 中的“被选中”状态来确定背景颜色。
    • 感谢您的回复。你有什么关于你所说的步骤的简短示例代码对我有很大帮助吗?
    • 我会在我测试完后 ping 你
    • 如果设计和方法合理,您会考虑支持并接受这个答案吗?谢谢!
    猜你喜欢
    • 2015-09-27
    • 2017-09-21
    • 1970-01-01
    • 2020-08-22
    • 1970-01-01
    • 1970-01-01
    • 2018-07-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多