【问题标题】:Changing button in cell when pressed按下时更改单元格中的按钮
【发布时间】:2015-09-07 08:44:13
【问题描述】:

我正在尝试更改按钮按下时的颜色。目前它在一个表格视图单元格中。

我这样做的方式是这样添加:

@IBAction func upVote(sender: AnyObject) {
        sender.setImage(UIImage(named: "bUpVote"), forState: .Normal)
    }

这是在单元类(不是视图控制器类)中完成的。

它有效,但更改也适用于表格其余部分的每三个单元格。 有什么解决办法吗?谢谢!

【问题讨论】:

  • 它是因为 tableview 的可重用功能
  • 请分享您的代码cellForRowAtIndexPath
  • 您可以在模型对象中保持按钮 upVote 状态,然后它将仅显示这些单元格的更改图像。

标签: ios swift


【解决方案1】:

解决这个问题的方法有很多,其中一种方法如下

将此添加到您的 customCell 类中,

@objc protocol MyTableViewCellDelegate {
func controller(controller: MyTableViewCell, button: UIButton, selectedButtonIndexPath : NSIndexPath)
}


class MyTableViewCell: UITableViewCell {
var delegate: AnyObject?
var indexPath : NSIndexPath?
@IBOutlet weak var button: UIButton!//outlet of button

按钮操作

@IBAction func buttonAction(sender: UIButton)//IF the sender type is AnyObject, you have to change it as UIButton
{
    self.delegate?.controller(self,  button: sender, selectedButtonIndexPath: indexPath!)
}

将此添加到具有UITableViewViewController 类中

 class MyTableViewController: UITableViewController, MyTableViewCellDelegate {  // I created a subClass of UITableViewController, your's may be different
var arraySelectedButtonIndex : NSMutableArray = []//global declaration

由于我使用 xib 创建了我的自定义单元格,viewDidLoad()

tableView.registerNib(UINib(nibName: "MyTableViewCell", bundle: nil), forCellReuseIdentifier: "CustomCell")//Since, I use custom cell in xib

通过添加这个来定义自定义单元格的委托

func controller(controller: MyTableViewCell, button: UIButton, selectedButtonIndexPath : NSIndexPath)
{
    if(arraySelectedButtonIndex .containsObject(selectedButtonIndexPath)==false)
    {
        arraySelectedButtonIndex.addObject(selectedButtonIndexPath)
        button.setImage(UIImage(named: "bUpVote")  , forState: .Normal)
    }
    else
    {
        arraySelectedButtonIndex.removeObject(selectedButtonIndexPath)//If you need to set Deselect image
        button.setImage(UIImage(named: "deselectImage") , forState: .Normal)//If you need to set Deselect image
    }
}

在tableView数据源(cellForRowAtIndexPath)

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

    let cell = tableView.dequeueReusableCellWithIdentifier("CustomCell", forIndexPath: indexPath) as! MyTableViewCell
    cell.delegate = self
    cell.indexPath = indexPath
    if(arraySelectedButtonIndex .containsObject(indexPath))
    {
        cell.button.setImage(UIImage(named: "bUpVote"), forState: .Normal)
    }
    else
    {
        cell.button.setImage(UIImage(named: "deselectImage"), forState: .Normal)//If you need to set Deselect image
    }
    return cell
}

【讨论】:

  • 谢谢!这很完美。不过我有一个问题:我在同一个单元格中有第二个按钮,每次单击第一个按钮时都必须更改(downvote 按钮)。我看到您正在发送带有“按钮:发件人!”的点击按钮。但是我将如何通过另一个按钮?
  • @ChrisJones 您必须创建另一个函数,如 func controller(controller: MyTableViewCell, **button2**: UIButton, selectedButtonIndexPath : NSIndexPath) 或另一种方式,全局声明 downVoteButton 并将现有函数作为参数传递..
【解决方案2】:

这是因为单元格被 tableView 重用。如果需要在单元格中保持子视图的状态,则需要更新数据源并反映 cellForRowAtIndexPath 方法中的更改。

【讨论】:

    【解决方案3】:

    这不是这样做的方法。您将按钮的状态存储在模型中。例如:说将项目的投票状态存储在您的模型中:

    class Post
    {
    var title : String
    var upvoted : Bool
    }
    

    如何获取索引路径?

    在您的自定义 tableview 子类上移动 IBAction 方法。在单元格中添加一个名为 delegate 的属性,并将其设置为 cellForRowAtIndexPath: 中的控制器。现在在 action 方法中通知委托。

    我在这里详细描述了这一点:https://stackoverflow.com/a/32250043/1616513

    现在当用户支持你更新模型时:

    @IBAction func upVotedInCell(sender: UITableViewCell) {
    
         var indexPath = self.tableView.indexPathForCell(sender)
    
         self.items[indexPath].upvoted = true
    
         self.tableView.reloadRowsAtIndexPaths([indexPath],UITableViewRowAnimation.None)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-23
      • 1970-01-01
      • 1970-01-01
      • 2011-09-23
      • 1970-01-01
      • 2013-06-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多