[英]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.