【发布时间】:2016-11-10 07:13:11
【问题描述】:
我正在尝试在tableview 单元格中为我的项目创建类似按钮。如果单击类似按钮,它必须将色调颜色更改为红色。我正在使用 Alamofire,如果用户喜欢它,它会返回最喜欢的提要并在单元格中:
let likerHash = data[indexPath.row]["liker_hash"] as? String
if(likerHash == ""){
cell.likeButton.tintColor = UIColor.blackColor()
}else{
cell.likeButton.tintColor = UIColor.redColor()
}
将为提要设置按钮颜色。但是,如果我单击一个按钮喜欢它,如果它不喜欢它会改变颜色,并且当我向下滚动并再次返回时,按钮颜色将再次变为以前的颜色。 (如果在加载数据时不喜欢它会在我向下滚动时保持颜色。)我很想改变liker_hash 的值,但它给了我一个错误:mutating method sent to immutable object。我想改变价值,比如:
self.data[sender.tag]["liker_hash"] = ""
我的data 来自[NSMutableDictionary]() 类型。知道如何用 swift 语言做到这一点吗?
【问题讨论】:
-
您的 tableView 是否重用了它的单元格?这可能是为什么当您向上滚动时按钮会重置的问题。
-
我使用
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomCell}做到了。 @The_Curry_Man -
我添加了一个答案。让我知道是否有帮助
标签: ios swift uitableview alamofire