common
common

Reputation: 49

change tableview cell content on collection view cell selection

I have a tableView and collectionView in one view controller.

in tableView I have title description and in collectionView I have lable .

I want on collectionView label selection tableView content should change .

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return Bookmark.count
    }


func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionViewBookmark.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! BookMarkCollectionViewCell



    cell.lblTitle.text = Bookmark[indexPath.row]
    cell.backgroundColor = UIColor.white



    return cell
}


func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let cell = collectionViewBookmark.cellForItem(at: indexPath)

        cell?.backgroundColor = UIColor.blue

        self.selectedIndexPath = indexPath
//
        let newsDict = arrNewsData[indexPath.row]

        if (indexPath.row == 1)
        {
        let cell1 = tableViewBookMark.cellForRow(at: indexPath) as! BookMarkFirstTableViewCell
        cell1.lblTitle.text = newsDict["title"] as! String
        tableViewBookMark.reloadData()
        }
        tableViewBookMark.reloadData()
    }

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableViewBookMark.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! BookMarkFirstTableViewCell

        let dict = arrNewsData[indexPath.row]


        cell.lblTitle.text = dict["title"] as! String
      // cell.imgBookMark.image = dict["image_url"]
        let url = URL(string: dict["image_url"] as! String)
        URLSession.shared.dataTask(with: url!) { (data, response, error) in
            if data != nil{
                DispatchQueue.main.async {
                    let image = UIImage(data: data!)
                    cell.imgBookMark.image = image
                }
            }
        }.resume()
         return cell
    }

Upvotes: 0

Views: 2455

Answers (2)

Parth
Parth

Reputation: 626

See my inline comments.

var tempCell: BookMarkFirstTableViewCell?

//Inside cellForRowAt indexPath

tempCell = cell
//Inside (collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)

tempCell.lblTitle.text = newsDict["title"] as! String

Upvotes: 1

Mohmmad S
Mohmmad S

Reputation: 5088

You're reloading the tableView after updating the values in the cell,

tableViewBookMark.reloadData() 

This will trigger the data source function including cellForRowAt, so you will lose your updated values a solution to this is to have a global variable in the UIViewController and check its values inside the cellForRowAt and update it in the collectionView DidSelect .

Extra tip: you don't need to reload all the tableView for a single change you can use

tableView.reloadRows(at: [indexPath], with: .top) 

to reload only number of selected cells in the tableView

Upvotes: 1

Related Questions