如何在另一個單元格被點擊后操作UITableViewCell ?- - - - - -斯威夫特

[英]How to manipulate UITableViewCell after another cell is tapped ? - Swift


How to manipulate UITableViewCell after another cell is tapped ?

如何在另一個單元格被點擊后操作UITableViewCell ?

I have 3 cells each one has UIPickerView the first cell's userInteractionEnabled is true but the 2nd and 3rd is false .. when the user tap the 1st cell the rest of cells's userInteractionEnabled should be true

我有3個單元格每個單元格都有UIPickerView第一個單元格的userInteractionEnabled是true但是第二個和第三個單元格是假的。當用戶點擊第一個單元時,其他單元格的userInteractionEnabled應該為true

I know I need to use userInteractionEnabled but how ? should I hold the cells in variables then manipulate when I need to?

我知道我需要使用userInteractionEnabled,但是怎么用呢?我應該在變量中保存單元格,然后在需要時進行操作嗎?

3 个解决方案

#1


1  

I've read solutions above and are all valid of course, but I prefer a solution like this: I suppose you have an array of object to display (ignoring pattern you're using).

我讀過上面的解決方案,當然都是有效的,但是我更喜歡這樣的解決方案:我假設您有一個要顯示的對象數組(忽略您正在使用的模式)。

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()
    }
}

I think this solution is more scalable and maintainable, but is my opionion.

我認為這個解決方案更具有可擴展性和可維護性,但這是我的鴉片。

update

For manipulate cell outside of 'cellForRowAtIndexPath' function, you can do in this way for instance:

對於“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
    }
}

#2


0  

Use a variable to keep track of userInteractionEnabled

使用一個變量來跟蹤啟用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()
    }
}

#3


0  

You could create a variable that holds on to the selection status of the first item:

您可以創建一個變量來保持第一項的選擇狀態:

var didSelectFirst = false

Then, in tableView(_:didSelectRowAtIndexPath:) you can tell the tableview to reload entirely, or just reload the two rows you want to reload.

然后,在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)
}

And in tableView(cellForRowAtIndexPath:) you can use the didSelectFirst variable to change userInteractionEnabled

在tableView(cellForRowAtIndexPath:)中,您可以使用didSelectFirst變量來更改userInteractionEnabled。

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

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2016/08/24/72a11f5ad066eb094f1881d733fa49a3.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com