4

テーブルビューの各セルにいいねボタンを作成しようとしています。押すとボタンが「いいね」に変わります。サブクラスにIBOutletを作成し、tableviewcontrollerクラスにsender.setTitle("unlike",forState: UIControlState.Normal)を使用してIBActionメソッドを作成することで、これを行うことができました。しかし、私がそれをクリックすると、メソッドは他のテーブルビューセルのボタンの束も同様に「好きではない」に変え、本質的に1つのセルの動作を複製します. これを行う方法は、1 つおきのセルを変更することです。そのため、2 つの連続したセルの「いいね」ボタンをクリックすると、テーブル ビュー内のすべてのセルが「いいね」に変わります。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 クラスの私のコードがあります:

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
    }

}

また、これは関係ありませんが、これを読んでいる人が私のスタイルやコードの明快さを改善する方法を教えてくれたら、それもありがたいです.

4

1 に答える 1

13

UITableViewCellは再利用可能です。これは、各セルのタイトルを「似ていない」または「好き」に設定する必要があることを意味します。とにかくデータを読み込むと思うので、最も簡単な方法は、文字列の配列を作成することですViewController

これをあなたに追加してくださいViewControllervar likes: [String]!

ViewDidLoad: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)
}
于 2015-08-13T00:44:54.177 に答える