简体   繁体   中英

how to access the cell content outside the function

Hi I need to call the cell label outside of the cellForRowAt indexPath function but it show the error any one help me to solve this issues here is my code:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {

        let cell: FeedCell = tableView.dequeueReusableCell(withIdentifier: "VedioCell", for: indexPath) as! FeedCell

        cell.slider.addTarget(self, action: #selector(handleVideoChageSlider), for:.valueChanged)
         cell.slider.tag = indexPath.row
   cell.videolabl.text="check"
          return cell
    }

 @IBAction func GetUpdatesButtonTouched(_ sender: AnyObject) {
//how do i call the cell fuction 
 //cell.slider.addTarget(self, action: #selector(handleVideoChageSlider), for:.valueChanged )
}

I need to call this cell.label to inside of the fuction of:

@IBAction func handleVideoChageSlider(_ sender: UISlider)  {
    print(sender.value)
//like cell.videolabel.text="change name"
}

Thanks in advance

To access Cell Content

Please assign indexPath with section no as well item No.

 let index = NSIndexPath(forItem: 0, inSection: 0)
 let refCell = tblListingURL.cellForRowAtIndexPath(index) as! FeedCell
 refCell.videolabl.text = "NewText"

You can use below code to get the cell content:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{

  let cell: FeedCell = tableView.dequeueReusableCell(withIdentifier: "VedioCell", for: indexPath) as! FeedCell

  cell.slider.addTarget(self, action: #selector(handleVideoChageSlider), for:.valueChanged)
  cell.slider.tag = indexPath.row
  cell.videolabl.text="check"
  return cell
}

@IBAction func handleVideoChageSlider(_ sender: UISlider)  {
  print(sender.value)

  //tableView is your table view object
  let cell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: sender.tag, inSection: 0)) as! FeedCell

  //now you can get your label
  cell.Yourlabel.text = "Hello India" 
}

You can access your cell content outside the function like this :

let indexPath = tableView.indexPathForSelectedRow!
let currentCell = tableView.cellForRowAtIndexPath(indexPath)! as! FeedCell

print(currentCell.videoLabel.text)

You need to make a cell in the function as below:

@IBAction func handleVideoChageSlider(_ sender: UISlider)  {
    print(sender.value)

    let cell: FeedCell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: sender.tag, inSection: 0)) as! FeedCell

    cell.videolabl.text="change name"
}

I've not checked all answers instead ,I tried. Not all worked due to not getting indexpath,

In this case with swift 5,Xcode 10.3 one thing worked for me. Let me know if something wrong, Thankyou.

let cell: cellCarerDailySchedual = tblDailySchedual.cellForRow(at: NSIndexPath(row: sender.tag, section: 0) as IndexPath) as! cellCarerDailySchedual

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM