【问题标题】:change button on click inside tableview cell在tableview单元格内单击更改按钮
【发布时间】:2016-03-13 23:53:36
【问题描述】:

我有一个表格视图单元格,每个单元格中都有类似按钮。

我想让用户点击单元格的like按钮,它会改变按钮图像。

这是我的代码:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Result", forIndexPath: indexPath) as! ResultTableViewCell
    cell.likebutton.tag = indexPath.row
    return cell
}

和功能

 @IBAction func printTag(sender: AnyObject) {
     let cell = tableView.dequeueReusableCellWithIdentifier("Result", forIndexPath: indexPath) as! ResultTableViewCell

     cell.likebutton.setImage(UIImage(named: "clicklike"), forState: UIControlState.Normal)
 }

有什么办法吗?

【问题讨论】:

  • 解决什么问题?你还没有说明你的问题是什么。
  • 我的问题是,当我点击按钮时,图像不会改变,功能也不起作用:(

标签: ios swift uitableview


【解决方案1】:

cellForRowAtIndexPath 之外,您永远不应从任何地方致电dequeueReusableCellWithIdentifier。流程是:

  1. 点赞按钮被点击
  2. @IBAction 处理程序因此触发
  3. 处理程序查找标签号,更新模型以指示它处于“喜欢”状态(表的支持数据 - 你的模型在哪里?你可能还不明白如何创建和管理一个。)
  4. 处理程序使用适当的NSIndexPath 调用table.reloadRowsAtIndexPaths()
  5. 这会传播到 iOS 调用 cellForRowAtIndexPath代表您,然后需要咨询您的模型,注意该特定行处于“喜欢”状态,并以不同方式呈现单元格内的图像, 在返回单元格之前。

根据您的上述尝试,这可能比您想象的要多得多。现在这对您来说有多可行?

编辑

我发现您仍在寻找捷径,而不是对数据进行建模。你需要这样的东西:

// This is a data model. I'm not sure you understand how critical this is.
var isLikedAtIndex = [ false, false, false ] // Example only: array of 3 booleans

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Result", forIndexPath: indexPath) as! ResultTableViewCell
    if isLikedAtIndex(indexPath.row) {
        cell.likebutton.setImage(someLikedImage)
    }
    else {
        cell.likebutton.setImage(someNotLikedImage)
    }
    cell.likebutton.tag = indexPath.row
    return cell
}


@IBAction func clickLike(sender: UIButton) {
    isLikedAtIndexPath(sender.tag) = true // !! The model !! Save your data!
    let myClickedIndexPath = NSIndexPath(forRow: sender.tag, inSection: 0)
    table.reloadRowsAtIndexPaths([myClickedIndexPath], withRowAnimation: .Automatic)
}

【讨论】:

  • IBAction 处理程序因此触发,喜欢你的回答;) IBAction func printTag(sender: UIButton) { sender.setBackgroundImage(UIImage(named: "selecetedCompare"), forState: .Normal) }跨度>
  • 对于即时短期来说,这是评论中的一个聪明的捷径,但这让我感到难过,因为最终要让应用程序做任何真实的事情,它需要一个代表数据独立于 UI。你正在进入死胡同。当你真的需要告诉数据库哪些项目被喜欢时会发生什么?
  • 另外,您的答案实际上是错误的,因为随着表格的增长,单元格将reused 用于表格的不同行*。因此,您从未“喜欢”过的随机单元格将显示为“喜欢”。但我希望你接受我的回答表明这正是你的目标,我的批评是为了拯救你的应用,而不是刻薄。
  • cell.likebutton.tag = indexPath.row
  • 好吧,这样做怎么样:P if let _ = compareId.indexOf({$0 == SearchResult[indexPath.row]["ID"].int!}) { cell.buttontag.setBackgroundImage( UIImage(named: "selecetedCompare"), forState: .Normal) }
【解决方案2】:

sender 应该是对 likebutton 的引用,因此您应该能够执行以下操作:

sender.setImage(UIImage(named: "clicklike"), forState: UIControlState.Normal)

我认为你这样做是错误的。当单元格被重复使用时,likebutton 最终会处于新 indexPath 的错误状态。 likebutton的状态应该在

中确定

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

tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath)

因此,您不应该为了像这样更改按钮而将单元格出列。相反,您应该更改确定按钮状态的属性,然后调用reloadRowsAtIndexPaths(indexPaths: [NSIndexPath], withRowAnimation animation: UITableViewRowAnimation)

我不确定您从哪里获得 indexPath,但您可以使用 sender 来确定它,如下所示:

let rootViewPoint = sender.superview?.convertPoint(sender.center, toView: self.tableView.center)
let indexPath = self.tableView.indexPathForRowAtPoint(rootViewPoint)

然后,您可以使用该 indexPath 来确定需要更改模型的哪个实例,进行更改,然后调用:

self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic) 

【讨论】:

    猜你喜欢
    • 2016-04-21
    • 1970-01-01
    • 1970-01-01
    • 2019-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多