【发布时间】:2016-06-17 20:42:32
【问题描述】:
我正在尝试构建一个内部带有不同按钮的自定义单元格,但是当我在单元格中点击“关注按钮”并为该按钮着色时,似乎其他关注按钮也获得了颜色......另外,如果我向上滚动然后在我的 tableView 下其他按钮随机获得颜色...我仍在学习如何使用自定义单元格...
这是我的自定义单元格
class customCell: UITableViewCell
{
@IBOutlet weak var follow: UIButton!
@IBOutlet var comment: UIButton?
@IBOutlet var share: UIButton?
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cellPost = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! customCell
cellPost.tag = Array(postToPrint.keys)[indexPath.row]
cellPost.follow?.tag = Array(postToPrint.keys)[indexPath.row]
cellPost.follow?.addTarget(self, action: "follow:", forControlEvents: .TouchUpInside)
return cellaPost
}
}
这里有我的功能关注
func follow(sender: UIButton)
{
let postSelected = sender.tag
// In order to get indexPath from int values
let indexPath = NSIndexPath(forRow: postSelected, inSection: 1)
let cell = table.cellForRowAtIndexPath(indexPath) as! customCell
cell.follow.setTitleColor(UIColor.redColor(), forState: UIControlState)
idPostClicked = postSelected
sendHttpRequest(postClicked : idPostClicked)
}
我将我的字典键作为标签给每个单元格。稍后我想将 cell.tag 作为参数发送到 sendHttpRequest 方法中。如果我将按钮功能放在我的 customCell 中,我想获取包含我的 followButton 的单元格,这样我就可以使用 cell.tag 发送请求。 但是,当我仅单击一个时,其他单元格中的文本会被着色...:/
【问题讨论】:
标签: ios swift uitableview cocoa-touch reusability