【问题标题】:How to manipulate UITableViewCell after another cell is tapped ? - Swift点击另一个单元格后如何操作 UITableViewCell ? - 斯威夫特
【发布时间】:2016-08-24 07:51:27
【问题描述】:

点击另一个单元格后如何操作UITableViewCell

我有 3 个单元格,每个单元格都有 UIPickerView 第一个单元格的 userInteractionEnabledtrue 但第二个和第三个是 false .. 当用户点击第一个单元格时,其余单元格的 userInteractionEnabled 应该是true

我知道我需要使用userInteractionEnabled,但是如何使用?我应该将单元格保存在变量中,然后在需要时进行操作吗?

【问题讨论】:

  • 点击表格视图行时是否要勾选类似 tik 标记?

标签: ios swift uitableview


【解决方案1】:

我已经阅读了上面的解决方案并且当然都是有效的,但我更喜欢这样的解决方案:我想你有一个要显示的对象数组(忽略你正在使用的模式)。

class MyObject: NSObject {
   var selected: Bool = false
}

class MyViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var objects: [MyObject] = []

    override func viewDidLoad() {
        super.viewDidLoad()
        // init your objects: 
        // 1stObj.enabled = true
        // 2ndObj.enabled = false
        // 3rdObj.enabled = false
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
         let obj = self.objects[indexPath.row]
         let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
         cell.userInteractionEnabled = obj.enabled
         return cell
    }

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        for obj in self.objects {
           obj.selected = !obj.selected
        } 
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
        tableView.reloadData()
    }
}

我认为这个解决方案更具可扩展性和可维护性,但这是我的意见。

更新

对于 'cellForRowAtIndexPath' 函数之外的操作单元格,您可以这样做,例如:

func manuallyModifyCell(atIndex index: Int, backgroundColor: UIColor = .clearColor()) {
    let indexPath = NSIndexPath(forRow: index, inSection: 0)
    if let cell = tableView.cellForRowAtIndexPath(indexPath) {
        cell.backgroundColor = backgroundColor
    }
}

【讨论】:

  • 谢谢你的回答,但是你能告诉我我是否改变了 cellForRowAtIndexPath 中不可选择的单元格的背景颜色。如何在点击第一个单元格后再次使颜色变为白色?我的意思是当我在 cellForRowAtIndexPath 乐趣之外时如何操作单元格对象
【解决方案2】:

使用变量来跟踪 userInteractionEnabled

var selectable: Bool = false

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    cell.textLabel?.text = sections[indexPath.row]
    if selectable {
        cell.isUserInteractionEnabled = true
    } else {
        if indexPath.row != 0 { // Only first cell is enabled
            cell.isUserInteractionEnabled = false
        }
    }

    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print(indexPath.row)
    if indexPath.row == 0 {
        selectable = true
        tableView.reloadData()
    }
}

【讨论】:

  • IMO 最好重新加载已更新的单元格(请参阅我的答案)。否则,这个方案和我提出的方案基本一样。
【解决方案3】:

您可以创建一个变量来保持第一项的选择状态:

var didSelectFirst = false

然后,在 tableView(_:didSelectRowAtIndexPath:) 中,您可以告诉 tableview 完全重新加载,或者只重新加载要重新加载的两行。

if indexPath.row == 0 {
    didSelectFirst = true
    // reload all
    tableView.reloadData()
    // reload some
    tableView.reloadRowsAtIndexPaths([
        NSIndexPath(forRow: 1, inSection: 0),
        NSIndexPath(forRow: 2, inSection: 0)], withRowAnimation: .Automatic)
}

tableView(cellForRowAtIndexPath:) 中,您可以使用didSelectFirst 变量来更改userInteractionEnabled

if indexPath.row == 1 || indexPath.row == 2 {
    cell.userInteractionEnabled = didSelectFirst
}

【讨论】:

  • 谢谢你的工作,但仍然存在问题,因为在 tableView(cellForRowAtIndexPath:) 我使用了 cell.backgroundColor=UIColor.clearColor() ,所以现在我怎样才能让单元格再次变白?跨度>
  • cell.backgroundColor = UIColor.whiteColor() 会使单元格再次变白
  • 我希望它们在启用后再次变白
  • if didSelectFirst { cell.backgroundColor = UIColor.whiteColor() } 或类似的东西。 didSelectFirst 跟踪是否选择了第一个单元格。
  • 我知道,但我的意思是当我在 didSelectRowAtIndexPath 中时如何操作“单元格”?我应该在 didSelectRowAtIndexPath 中再次使用 cellForRowAtIndexPath 吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-22
  • 2017-03-24
  • 1970-01-01
  • 2020-06-02
  • 2023-03-31
相关资源
最近更新 更多