在swift中的tableviewCell中实现like按钮 [英] Implementing a like button in a tableviewCell in swift
问题描述
我想在每个tableview单元格中创建一个类似的按钮。按下时,按钮将变为不同。我能够通过使用sender.setTitle(不同于,forState:UIControlState.Normal)在我的子类中创建IBOutlet和在tableviewcontroller类中创建IBAction方法来实现此目的。但是当我点击它时,该方法将一堆其他tableviewcell的按钮变为不同,基本上复制了一个单元格的行为。这样做的方式是它会改变其他所有单元格,所以如果我单击2个连续单元格的like按钮,表格视图中的所有单元格将变为不同。这是我的tableViewController的代码:
I am trying to make a like button in each of my tableview cells. When it is pressed, the button will change to "unlike". I was able to do this by creating an IBOutlet in my subclass and an IBAction Method in my tableviewcontroller class using sender.setTitle("unlike",forState: UIControlState.Normal). But when I click it, the method turns a bunch of other tableviewcell's button's to "unlike" as well, essentially duplicating the behavior of one cell. The way it does this is that it changes every other cell, so if I click the "like" button of 2 consecutive cells, all cells in the table view will turn to "unlike". Here is my code for the tableViewController:
class TableViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 30
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as TableViewCell
cell.tag = indexPath.row
cell.like.tag = indexPath.row
cell.like.addTarget(self, action: "handleLikes:", forControlEvents: .TouchUpInside)
return cell
}
@IBAction func handleLikes(sender: AnyObject){
println(sender.tag) // This works, every cell returns a different number and in order.
sender.setTitle("Pressed", forState: UIControlState.Normal)
}
这是我的TableViewCell类的代码:
And here is my code for the TableViewCell class:
class TableViewCell: UITableViewCell {
@IBOutlet weak var like: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
此外,这是无关的,但如果有人读到这个可以告诉我如何我可以改进我的风格和/或代码清晰度,我也很感激。
Also, This is unrelated, but if anyone reading this could tell me how I could improve my style and/or clarity of code, I would appreciate that as well.
推荐答案
UITableViewCell
是可重用的。这意味着您必须为每个单元格将标题设置为不同或相似。最简单的方法,因为我想你无论如何都会阅读数据,就是在你的 ViewController中创建一个字符串数组
UITableViewCell
s are reusable. This means that you must set the title to "unlike" or "like" for each cell. Easiest way, since I suppose you will be reading in data anyways, would be to create an array of Strings in your ViewController
将此添加到 ViewController
: var likes:[String]!
: likes = [String](count:20,repeatedValue:like)
请注意,长度应该基于您将显示的 UITableViewCells
的数量。
您的 cellForRowAtIndexPath
:
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as TableViewCell
cell.like.tag = indexPath.row
cell.like.addTarget(self, action: "handleLikes:", forControlEvents: .TouchUpInside)
cell.like.setTitle(likes[indexPath.row], forState: UIControlState.Normal)
return cell
}
handleLikes
功能:
func handleLikes(sender: AnyObject){
println(sender.tag) // This works, every cell returns a different number and in order.
if likes[sender.tag] == "like" {
likes[sender.tag] = "unlike"
}
else {
likes[sender.tag] = "like"
}
sender.setTitle(likes[sender.tag], forState: UIControlState.Normal)
}
这篇关于在swift中的tableviewCell中实现like按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!