1

I have a table view loaded with 4 rows inside.

Row 3 will have a checkbox. I want to enable Row 4 only when Row3 check box is checked

so I have an outlet for checkbox (which is button) and action on tap of that checkBox.

My question is how can i get reference to next cell and make it enable/diable

I tried to get reference of that cell as myTableView.cellForRow(at: index + 1) but it throws error that Binary operand cannot be applied to indexPath.

Pls suggest how i can achieve

Ashh
  • 569
  • 1
  • 6
  • 28

4 Answers4

1

I assume you have your own custom UITableViewCell class. And your MyTableViewController is an delegate for this tableview cell. Whenever user check/uncheck, your box delegate tells your controller about it and you invoke reload method to get rid of given row.

That's solution I am using in my app.

import Foundation
import UIKit

class MyTableViewController: UITableViewController {
    private struct CurrentSetting {
        var RowAEnabled: Bool = true
        var RowBEnabled: Bool = true
        var RowCEnabled: Bool = true
        var RowDEnabled: Bool = true
        var RowEEnabled: Bool = true
        //...
        //maybe more?
    }


    /// Let's say those are your table view rows. Data or whatever.
    fileprivate var settings: [String] {
        //In here you have your static position.
        var configuration = ["Row A","Row B", "Row C"]
        //However, if some data are enabled/disabled you modify it based on settings
        if current.RowAEnabled {
            configuration += ["Row D"]
        }
        return configuration
    }
    //This is your setting for current tableview
    private var current = CurrentSetting()
}

//Use this to reload row for your enable/disable cell.
tableView.reloadRows(at: [indexPath], with: .none)

Example how did that work with my app. IMPORTANT: settings it's data source for your tableview. number of items.

enter image description here

Shial
  • 1,386
  • 19
  • 31
0

In action of that checkBox, you can perform reloadData of your tableview. After that, you can check inside cellForRowAtIndexPath

like:

if indexpath.row == 4  
then  
//just my opinion, you can do whatever you want.  
cell.userInteractionEnabled = checkBox.isSelected 

or you can check inside numberOfRowsInSection, you can remove it from dataSource.

Lan Tran
  • 11
  • 6
0

I will suggest the optimised and robust way.

If you have and Datasource Array to load in TableView then add one extra property named isEnabled in the Object.

Else

create a Array as follows

var cellStateArray = Array(repeating: true, count: numberOfRows)

then when you will toggle the check box depending on that toggle the flag for required cell index (currently it's row 4 for you) to false OR true in cellStateArray

And in the cellForRowAt Method check the flag in cellStateArray for the respective cell index and set userInteractionEnabled enable/disable (extra configuration if you want)

And then simple reload the cell/table

Harshal Bhavsar
  • 1,598
  • 15
  • 35
  • ok, but let assume the cell enable/disable state determine it presents on a screen. In that case your solution does not help. The data are still there. So only what he can do is changing size to 0 which won't remove view from hierarchy. However. my solution allows to do that. – Shial Mar 01 '18 at 03:35
  • I don't think that will be the case just reloading the cell will help to update the state of cell – Harshal Bhavsar Mar 01 '18 at 04:09
0

Clicking on checkBox let's say have a method name checkBoxClicked. You can do like following:

@IBAction func checkBoxClicked(_ sender: Any) {
        let button = sender as! UIButton
        let clickedIndex = button.tag

        let nextIndexPath = IndexPath(item: clickedIndex+1, section: 0) // specify section here, if it's sectioned tableview
        let cell = tableView.cellForRow(at: nextIndexPath)

        // enable cell here
        cell?.isUserInteractionEnabled = true    // or reload cell
}

Do not forget to set tag to your checkbox in cellForRowAtIndexPath method like:

cell.checkBoxButton.tag = indexPath.row
rushisangani
  • 3,195
  • 2
  • 14
  • 18
  • I got an idea from this..how ever below check worked for me -----------if let secondCell = myTableView.cellForRow(at: IndexPath(row: (index + 1), section: 0)) as? MyCustomCell { – Ashh Mar 01 '18 at 07:48
  • @Ashh You are doing it in the hardcoded way. Which will limit you and you have to write more logic if you have to change – Harshal Bhavsar Mar 01 '18 at 08:03