ios - 如何在不创建 *Cell.swift 文件的情况下访问 UITableViewCell 对象

标签 ios xcode swift uitableview

我有带有动态单元格的 tableView。

tableViewCell 中,我有 2 个标签1 个按钮

在函数 cellForRowAtIndexPath 中,我通过这样的代码字符串向它的按钮添加操作:

btn?.addTarget(self, action: "downloadButtonClicked:", forControlEvents: .TouchUpInside)

我当然有这个功能

func downloadButtonClicked(sender: AnyObject){

}

在这个函数中,我可以通过以下方式访问它的 Cell 按钮

(sender as UIButton)?.hidden = true //for example

但是我如何从这个函数访问这些标签 - downloadButtonClicked

它们有特定的标签值,但我不知道如何访问它们。许多实现“viewWithTag”的变体都不让我满意

我需要变体而不需要为此单元格创建特定文件“MyTableViewCell.swift”

最佳答案

首先像这样找到按钮索引:

var btnPos: CGPoint = sender.convertPoint(CGPointZero, toView: self.tableView)
var indexPath: NSIndexPath = self.tableView.indexPathForRowAtPoint(btnPos)!

然后使用我们刚刚得到的indexPath创建cell

 cell  = tableView.cellForRowAtIndexPath(indexPath) as! YourTableViewCellClass

然后您可以使用

访问您的标签
 cell.yourLabel.text = "anything"

关于ios - 如何在不创建 *Cell.swift 文件的情况下访问 UITableViewCell 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33214915/

相关文章:

ios - 奇怪的自定义 UITableViewCell 行为

ios - 带有 [NSNull null] 的 NSMutableArray 与 NSPointerArray

ios - 在使用 WatchConnectivity 将数据从 WatchOs 传输到 iOs 时,是否有任何代表来监控进度?

ios - 如何在 Apple Watch 中打开具有特定位置的 map 应用程序?

objective-c - Storyboard中的组静态 tableView 未出现

ios - 动态 CollectionView 依赖于 SWIFT 4.0 中的 TableViewCell 值

ios - 如何重新加载和重置 UIPickerView 中的行数 - Swift

javascript - 如何在iOS6中关闭选择器onchange事件的下拉菜单?

ios - 使用 CXStartCallAction 请求事务时出错

php - 使用 EasyApns 添加推送通知