【发布时间】:2016-01-24 21:41:50
【问题描述】:
我有一个 tableView,我在其中发布用户提要,并在单元格视图中添加了一个心形按钮。我为单元格视图创建了一个类,并在那里声明了我的按钮的@IBOutlet。然后在表格视图的cellForRowAtIndexPath 中,我调用了按钮并将indexpath.row 设置为按钮本身的标记号。然后我添加了一个要完成的操作的目标并创建了我的@IBAction。现在我正在尝试将心形按钮的图像外观更改为红色,但没有任何反应。通过发送方将 UIImage 传递给按钮是否存在问题。我没有错误。 if like = 语句工作正常。这是我的代码:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! PostsCellTableViewCell
cell.heartButton.tag = indexPath.row
cell.heartButton.addTarget(self, action: "liked:", forControlEvents: .TouchUpInside)
return cell
}
@IBAction func liked (sender: UIButton){
if like == false{
sender.imageView?.image = UIImage(contentsOfFile: "red-heart.png")
like = true
}
else{
sender.imageView?.image = UIImage(contentsOfFile: "white-heart-hi.png")
like = false
}
// self.tableView.reloadData()
}
【问题讨论】:
标签: ios swift uitableview uibutton