繁体   English   中英

按下时更改单元格中的按钮

[英]Changing button in cell when pressed

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

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

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

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

它有效,但更改也适用于表格其余部分中跟随它的每三个单元格。 任何解决办法? 谢谢!

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

将此添加到您的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!)
}

将此添加到具有UITableView ViewController

 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
}

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

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

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)
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM