2016-04-10 19 views
3

私は最近iOS開発を学び始めましたが、いくつか問題がありました。ボタンが押されたときにUITableviewCellでカスタムビューを表示する方法は?

ラベル、画像ビュー(デフォルトでは隠してあります)、およびボタンのカスタムセルを持つテーブルビューがあります。 ボタンをクリックして画像ビューを表示するようにしたいと思います。 問題は、セルを再利用するたびに、再利用されたセルの画像ビューが表示されることです。 最初のセルのボタンが押された場合は、最初のセルのイメージビューだけが表示されるようにしたいと思います。 1番目と3番目のセルのボタンを押すと、1番目と3番目の行だけが表示され、他の行は表示されません。

私の現在のソリューション:

class CustomTableViewCell: UITableViewCell { 

    @IBOutlet var cellTitleLabel: UILabel! 
    @IBOutlet var cellImageView: UIImageView! 
    var showImageView = false 


    @IBAction func showImageViewAction(sender: UIButton) { 
     showImageView = true 
     displayCell() 
    } 

    func displayCell() { 
     if showImageView { 
      cellImageView.hidden = false 
     } else { 
      cellImageView.hidden = true 
     } 
    } 
} 

とビューコントローラ用:セルが再利用されたときに、画像ビューが隠されますように作成する方法について

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 30 
} 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("CustomCell", forIndexPath: indexPath) as! CustomTableViewCell 

    return cell 
} 

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { 
    let customCell = cell as! CustomTableViewCell 
    customCell.displayCell() 
} 

任意の提案ですか?あなたがcellImageview.hidden状態を保存する必要がある場合

+0

に適合させるので、画像ビューは、すべての30個のセルに現れますか? –

+0

いいえ現在、最初のセルのボタンをタップすると、画像ビューには次のセルが表示されます:1,6,11,16,21、および26です。セル6,11,16,21 26はdequeueReusableCellWithIdentifierが – user3489053

+0

と呼ばれるときにセル1を再利用します。特定のセル[indexPath.row]を最初に取得してから、そのセルの画像を非表示にできますか? –

答えて

1

はちょうどので好きですか:

がactionButtonが押されていることMainClassを通知するためにプロトコルを追加:あなたのCustomTableViewCellに比べ

protocol customCellDelegate{ 
    func actionButtonDidPressed(tag: Int, value: Bool) 
} 

var delegate: customCellDelegate? 
を宣言します

および@IBAction func showImageViewAction(sender: UIButton)追加:

ボタンをタップすると、あなたのMAINVIEWで
@IBAction func showImageViewAction(sender: UIButton) { 
    cellImageView.hidden = ! cellImageView.hidden 
    delegate?.actionButtonDidPressed(self.tag, value: imageCell.hidden) 
} 

はそれがcustomCellDelegate

var status = [Bool]() 

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    let rowCount = 100 
    for _ in 0 ..< rowCount{ 
     status.append(true) 
    } 
    return rowCount 
} 


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! customcell 
    cell. cellImageView.hidden = true 
    cell. cellImageView.hidden = status[indexPath.row] 
    cell.delegate = self 
    cell.tag = indexPath.row 

    return cell 
} 

func actionButtonDidPressed(tag: Int, value: Bool) { 
    status[tag] = value 
} 
+0

この場合、テーブルビューの最後までスクロールしてからスクロールバックすると、イメージビューのステータスは保存されません。それは再び隠されるようになります。私はどこかのセルの画像ビューの状態を保存する必要があります。 – user3489053

+0

作業中の(テスト済みの)コードで自分の投稿を更新しました。何か他のものが必要な場合は教えてください –

+0

ありがとうございました。これは魅力のように機能します。コールバックを使用している私の現在のソリューションよりも優れています。 – user3489053

関連する問題