ios - 点击时突出显示表格 View 单元格,按下警报 Controller 操作按钮时取消突出显示

标签 ios swift uitableview uiactionsheet

我喜欢在用户触摸 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 但是当我点击另一个单元格时,前一个单元格变成并保持灰色

最佳答案

在 View Controller 中初始化:

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

在 TableView 初始化中: 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;

在警报 View 委托(delegate)中: [tableview deselectRowAtIndexPath:selectedIndexPath][tableview reloadIndexPath:lastSelection]

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

2) 默认高亮点击时单元格变为蓝色

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

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

关于ios - 点击时突出显示表格 View 单元格,按下警报 Controller 操作按钮时取消突出显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39938256/

相关文章:

ios - 如何为 iOS 版本 9 + 10(或许还有 8)实现 Apple 推送通知?

在 SpriteKit 中使用 Swift 的 IOS admob

swift - 无法在 prepareforsegue 上检索 indexPath?

ios - 使用 UITableView Cell 中的 segue 更新数据

ios - 在条件解包后解包可选时意外发现 nil

iOS 应用程序在下载非常大的文件时耗尽内存

ios - 搜索栏在 IOS 应用程序 swift 3 中不起作用

swift - 如何将生成协议(protocol)声明为委托(delegate)?

swift - XCode:我无法设置 tableView 的委托(delegate)(IB 或以编程方式)

ios - 类型 'MGLMapView?' 的值在设置 ClusterKit 时没有成员 'clusterManager'