如何访问函数外的单元格内容

时间:2016-12-15 06:58:40

标签: ios swift

您好我需要在cellForRowAt indexPath函数之外调用单元格标签,但它显示错误任何一个帮助我解决此问题 这是我的代码:

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 )
}

我需要将此cell.label称为以下内容:

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

提前致谢

5 个答案:

答案 0 :(得分:1)

访问单元格内容

请为indexPath指定no no no no。

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

答案 1 :(得分:1)

您可以使用以下代码获取单元格内容:

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" 
}

答案 2 :(得分:0)

您可以在此功能之外访问您的单元格内容,如下所示:

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

print(currentCell.videoLabel.text)

答案 3 :(得分:0)

您需要在函数中创建一个单元格,如下所示:

someFunctionThatTakesAClosure

答案 4 :(得分:0)

我没有检查所有答案,而是尝试了。由于未获取索引路径,因此并非所有方法都能正常工作

在这种情况下,快速的5,Xcode 10.3可以为我工作。让我知道是否有问题,谢谢。

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