0

I am using tableview with multiple section and my data is structured like array of array form so I need to pass two integer values while tapping from button but we can pass only value with the help of tag property, so how would I solve my problem please help me out.

var data = [[String: Any]]()

self.data.append(["day": "Mon",
                 "record": [
                    ["name": "Abhya", "status": false], ["name": "Anivesh" , "status": false]
                ]
            ])
    
    self.data.append(["day": "Tue",
                 "record": [
                    ["name": "Vivek" , "status": false], ["name": "Arun" , "status": false]
                ]
            ])



    func numberOfSections(in tableView: UITableView) -> Int {
       return data.count
    }

   func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: MyTableViewCell.identifier, for: indexPath) as! MyTableViewCell
    
    let dictionary = self.data[indexPath.row]
    let array = getArray(withDictionary: dictionary["record"])
    let dict = array[indexPath.row]
    cell.myLabel.text = dict["name"] as? String ?? ""
    cell.tapButton.tag = indexPath.row
    
    if dict["status"] as? Bool ?? false {
        cell.tapButton.backgroundColor = .red
    } else {
        cell.tapButton.backgroundColor = .blue
    }
    return cell
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    let dictionary = self.data[section]
    let array = getArray(withDictionary: dictionary["record"])
    return array.count
}

@IBAction func buttonTapAction(_ sender: UIButton) {
    // want to cahnge the status false to true
}
  • Where is numberOfRows method ? – Shehata Gamal Apr 06 '21 at 09:06
  • Probably you should use indexPath.section instead of indexPath.row here let dictionary = self.data[indexPath.row] – stosha Apr 06 '21 at 09:10
  • then how would I access the current row element –  Apr 06 '21 at 09:12
  • I need to store both section and indexpath.row –  Apr 06 '21 at 09:12
  • for example let tag = indexPath.section * 1000000 + indexPath.row ... let row = Int(tag % 1000000);let section = (tag - row) / 1000000 – stosha Apr 06 '21 at 09:34
  • on my first comment i meant you will get runtime error if you use indexPath.row. – stosha Apr 06 '21 at 09:36
  • Note that many of the answers in the duplicate use either the tap point or `tag` to identify the index path. I do not advise doing this. Use either a delegate or a closure as in my answer or Jacob King's answer. – Paulw11 Apr 06 '21 at 11:02

1 Answers1

0

You seem to be a doing a lot of things in the Delegate class which are actually to be handled within MyTableViewCell

cell.myLabel.text = dict["name"] as? String ?? ""
 cell.tapButton.tag = indexPath.row
    
 if dict["status"] as? Bool ?? false {
     cell.tapButton.backgroundColor = .red
 } else {
     cell.tapButton.backgroundColor = .blue
 }

and

@IBAction func buttonTapAction(_ sender: UIButton) {
    // want to cahnge the status false to true
}

can be moved inside MyTableViewCell. You can have a method say setup(withDict: dict) and handle setting of data to cell within that. You can also maintain a copy of the dict within the cell, this way when the buttonTapAction will be called, you will know the status within the cell. You can then have a callback to the Delegate / ViewController to reload the cell with updated data.

Adithya
  • 4,545
  • 3
  • 25
  • 28
  • can you please define it code –  Apr 06 '21 at 09:18
  • Just add `setup(withDict: dict)` method in your `MyTableViewCell` to which you will pass your dictionary data; setting the name and backgroundColor to be done in that. Also `buttonTapAction` will also be within the `MyTableViewCell`. When `buttonTapAction` gets called you can directly change the background color of the tapButton in that. – Adithya Apr 06 '21 at 09:25
  • sorry not getting anything –  Apr 06 '21 at 09:33
  • how would I access cell within the TableViewCell class –  Apr 06 '21 at 09:42
  • Found a perfect article for you, refer this https://fluffy.es/handling-button-tap-inside-uitableviewcell-without-using-tag/ This is a very trial implementation, so don't depend too much on stackoverflow for this, you will find a lot of articles online for tableviewcell implementation. I have given you the right way to implement it, you should be able to build on from there. – Adithya Apr 06 '21 at 09:54
  • That article contains the two approaches I recommend from the linked duplicate; answers that predate that article by 2 years. Putting the dictionary (model) into the cell and manipulating it there is not the right approach. You will also have a problem since Swift dictionaries are value types. – Paulw11 Apr 06 '21 at 11:04
  • fluffy.es/ it's based on single section and too complex too sorry not useful need to checkout another article –  Apr 06 '21 at 11:10
  • problem resolved with help of delegate and protocol right resource https://www.youtube.com/watch?v=fzjtvq-jC4E –  Apr 06 '21 at 11:57