ios - 在自定义 UITableView 中--类型 'AnyObject->()' 的值没有成员 'tag' 错误

标签 ios swift uitableview

我在制作自定义 UITableViewCell 时遇到问题。

问题是,在我的自定义类中,我的按钮代码带有发件人“AnyObject”:

@IBAction func checkBox(sender: AnyObject) {
    }

在我的主 ViewController 中,我有更改单元格中某些内容的功能:

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

        let cell:ingredientCell = self.ingredientTable.dequeueReusableCellWithIdentifier("cell") as! ingredientCell

        cell.inCellLabel.text = self.items[indexPath.row]
        cell.checkBox.tag = indexPath.row
        cell.checkBox.addTarget(self, action: Selector("yourButtonClicked:"), forControlEvents: .TouchUpInside)

        return cell
    }

Xcode 在我的 cell.checkBox.tag 和 cell.checkBox.addTarget 行中说 Value of type 'AnyObject->()'has no member 'tag' 我真的无法理解,也找不到任何解决方案。

谢谢。

最佳答案

checkBox 是一个函数,您将其视为一个属性。如果您想要引用您的 checkBox View ,无论它是什么,您都需要为其创建一个 @IBOutlet 属性,该属性在 Storyboard 中连接并使用它而不是你的 @IBAction 函数。

代替

@IBAction func checkBox(sender: AnyObject) {}

你需要

@IBOutlet weak var checkBox: UIButton!

关于ios - 在自定义 UITableView 中--类型 'AnyObject->()' 的值没有成员 'tag' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33662020/

相关文章:

iphone - 具有动态高度的 UILabel 不适用于 iOS 中的相同字符串

ios - 如何从谷歌地图中的经纬度获取位置详细信息?

iphone - 如何在iOS应用程序中加载FNT格式的字体

swift - 我们可以在 watchOS 中重叠 UI 元素吗?

swift - 如何在 Swift 中打印未知类型的描述?

swift - 在使用 queryEqualToValue 查询获取用户信息时,我无法获取具有额外子 [Firebase][Swift] 的用户信息

ios - UITableView:如何调整包含自动换行标签的标题大小

ios - 如何以编程方式获取 iOS 上粘贴板内容的来源

ios滚动后保持UITableViewCell布局

ios - 通过单击单元格中的取消按钮来删除该单元格,也可以通过 editActionsForRowAt 方法删除该单元格