在按下警报控制器操作按钮时轻触并取消突出显示表格视图单元格

时间:2016-10-08 23:01:01

标签: 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但是当我点击另一个单元格时,前一个单元格转动并保持{{1} }

1 个答案:

答案 0 :(得分: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 set

中,则将重绘为灰色
相关问题