简体   繁体   English

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

[英]Changing button in cell when pressed

I'm trying to change the color of a button when pressed.我试图在按下时更改按钮的颜色。 Currently its inside a table view cell.目前它在一个表格视图单元格内。

The way I'm doing it is adding as so:我这样做的方式是这样添加:

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

and this is done inside the cell class (not the view controller class).这是在单元类(不是视图控制器类)内完成的。

It works, but the change also applies to every third cell that follows it for the rest of the table.它有效,但更改也适用于表格其余部分中跟随它的每三个单元格。 Any work around?任何解决办法? Thanks!谢谢!

There are many way to solve this issue, one of the method is as follows有很多方法可以解决这个问题,其中一种方法如下

Add this to your customCell class,将此添加到您的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

button Action按钮 动作

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

Add this to your ViewController class that has UITableView将此添加到具有UITableView ViewController

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

Since i created my custom cell using xib, in viewDidLoad()由于我使用 xib 创建了自定义单元格,因此在viewDidLoad()

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

define delegate of custom cell by adding this通过添加此定义自定义单元格的委托

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

In tableView dataSource ( cellForRowAtIndexPath )在 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
}

It's because cells are reused by the tableView.这是因为单元格被 tableView 重用。 if you need to persist the state of subviews in the cell, you need to update your data source and reflect the changes in cellForRowAtIndexPath method.如果需要在单元格中持久化子视图的状态,则需要更新数据源并反映 cellForRowAtIndexPath 方法中的更改。

This is not the way to do it.这不是这样做的方法。 You store the state of the button in your model.您将按钮的状态存储在模型中。 Eg: say store the item's upvoted status in your model :例如:说将项目的 upvoted 状态存储在您的模型中:

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

How to get the index path ?如何获取索引路径?

Move the IBAction method on your custom tableview subclass.在您的自定义 tableview 子类上移动 IBAction 方法。 Add a property called delegate to the cell and set it to your controller in cellForRowAtIndexPath: .向单元格添加一个名为 delegate 的属性,并将其设置为cellForRowAtIndexPath:的控制器。 Now in the action method inform the delegate.现在在 action 方法中通知委托。

I have described this in detail here : https://stackoverflow.com/a/32250043/1616513我在这里详细描述了这一点: https : //stackoverflow.com/a/32250043/1616513

Now when the user upvotes you update the model :现在,当用户投票时,您更新模型:

@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