Home > Mobile >  Swift - How to access index of UITableView after clicking on button in UICollectionView which is in
Swift - How to access index of UITableView after clicking on button in UICollectionView which is in

Time:02-04

I will repost my question in more detail.

I have a table view which contains some information.

Inside this table view, I have a nested collection view which contains one button for every cell. Hierarchy and example of app is shown below. The colorful buttons are the buttons in the collection view.

enter image description hereenter image description here

What I'm trying to achieve is I want to get the index of the UITableView row whenever a user clicks on a button inside the collection view. (I need this index to send data regarding the information on the table view row).

Initially, before adding a collection view, I simply had two buttons, and I was easily able to achieve this by using sender tags. However now that I'm using a collection view, I cannot use .addTarget for to it, and therefore can't make use of sender tags.

Maybe there's an easy way to do it but I'm a beginner in Swift so there might be something I'm missing.

I thought it would be as easy as declaring a variable in didSelectRowAt where the variable would be equal to indexPath.row, however didSelectRowAt doesn't work for me at all? It doesn't return anything

homework_history_cell which contains outlet for the collection view:

class homework_history_cell: UITableViewCell {

   @IBOutlet weak var homeworkMarksCollectionView: UICollectionView!

}

cellForItemAt method in my view controller file which is used for getting the sender tag of the clicked button:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

   let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellHomeworkMark", for: indexPath) as! homework_marks_collection_view_cell
        cell.markButton.tag = indexPath.row
        cell.markButton.setTitle(self.homeworkMarks[indexPath.row].value, for: .normal)
        cell.markButton.backgroundColor = markColors[indexPath.row % self.homeworkMarks.count]
        cell.markButton.addTarget(self, action: #selector(done_pressed(sender:)), for: .touchUpInside)
        cell.layer.cornerRadius = 10
        return cell

}

done_pressed method which sends the index of the table view row (this is the part that I'm stuck at)

@objc func done_pressed(sender: UIButton)
{
    evaluateHomework(homeworkIdx: homeworkIndex, operation: self.homeworkMarks[sender.tag].markIdx) // homeworkIndex is the table row index that I want to send when the user clicks on a button in the collection view. this does not work.
}

CodePudding user response:

add indexPath.row into tablecell

class homework_history_cell: UITableViewCell {

   @IBOutlet weak var homeworkMarksCollectionView: UICollectionView!
   var index: Int = -1
}

when you create cell - add value for index

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            if let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? homework_history_cell
            cell.index = indexPath.row
            ....
            return cell
        } else {
        return UITableViewCell()
        }
    }

and now you know index of selected cell in collectionView didSelect

CodePudding user response:

You can do this easily using closures:

class homework_history_cell: UITableViewCell {

   @IBOutlet weak var homeworkMarksCollectionView: UICollectionView!
   var onCollectionCellTap: (() -> ())?

   func collectionView(_ collectionView: UICollectionView, 
             didSelectItemAt indexPath: IndexPath) {
       onCollectionCellTap?()
   }

}

Now, in your class with tableView, inside cellForRow, write this:

tableCell.onCollectionCellTap = { [weak self] in
    // this will give you the tapped collection view's row
    print(indexPath.row)
}
  •  Tags:  
  • Related