2016-04-02 31 views
1

私はカスタム選択リストを作成しようとしていますが、これは単一の選択を許可するUITableviewを使用しています(ただし、必要なだけ多くのオプションを選択できます)。SwiftのUITableviewカスタムチェックボックスオプションのチェックを外します

私のカスタムセルにはLabelとImageViewが含まれています。必要なのは、IVに含まれる画像をタップで変更することだけです。

didSelectRowAtIndexPathで画像をチェックなしからチェックなしに変更することはできますが、問題を解決するには問題が発生しています。

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) 
{ 
    print("tableView -> didSelectRowAtIndexPath") 

    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! CustomCellVC 

    cell.backgroundColor = UIColor.clearColor() 
    cell.ivRiskCellImage.image = UIImage(named: "CheckedBox") 
} 

私はオンとオフに回し、次に強調表示された画像としてcheckedboxの画像を追加することとによって、ImageViewのの強調表示の状態でプレイしようとしたが、それは場合にのみ入るが、それ以外の場合は、再び

クリックした場合は無視します
if(cell.ivCellImage.highlighted == false) 
    { 
     print("Highligth is OFF") 
     cell.ivCellImage.highlighted = true 
    } 
    else if(cell.ivCellImage.highlighted == true) 
    { 
     print("Highligth is ON") 
     cell.ivCellImage.highlighted = false 
    } 

だけでなく、私はチェックを表示するために管理したときにブロックが完全に、また

if(cell.ivRiskCellImage.image!.isEqual(UIImage(named: "UncheckedBox"))) 
    { 
     print("Is unchecked!") 
     cell.ivRiskCellImage.image = UIImage(named: "CheckedBox") 
    } 
    else if(cell.ivRiskCellImage.image!.isEqual(UIImage(named: "CheckedBox"))) 
    { 
     print("Is checked!") 
     cell.ivRiskCellImage.image = UIImage(named: "UnheckedBox") 
    } 

を無視されている場合、他のこれは、IVの内側にあるものの画像をチェック私はこの

cell.ivRiskCellImage.image = UIImage(named: "CheckedBox") 

を追加することにより、cellForRowAtIndexPath内のすべてのセルのためにチェックボックスを設定してみてください場合は、チェックボックスのチェックマークは、もはや半透明である一方、クリックでedboxの画像は、私はこの

enter image description here

ような何かを得るませんが、それはすべてのアイデア

enter image description here

する必要がありますように、白?私は運が無ければこれを解決しようとかなりの時間をかけました。

答えて

3

代わりlet cell = tableView.dequeueReusableCellWithIdentifier...の私は

let cell = tableView.cellForRowAtIndexPath(indexPath) as! CustomCellVC 

はあなたが画像を変更したいセルへの参照を取得します。この方法を使用します。tableView.dequeueReusableCellWithIdentifierをデリゲートメソッドtableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)で使用されることを意味しているあなたは、あなたのをリサイクルしたいときより良いパフォーマンスのためのセル。

cell.ivRiskCellImage.image!.isEqual(UIImage(named: "UncheckedBox"))UIImage(named: "UncheckedBox")は、確認したいUIImageと同じではない新しいUIImageを作成するため、機能しません。

+0

ありがとうございました。私はこれで終わった:func tableView(tableView:UITableView、didSelectRowAtIndexPath indexPath:NSIndexPath) { let cell = tableView.cellForRowAtIndexPath(indexPath)as! (命名: "CheckedBox")RiskCustomCellVC cell.ivRiskCellImage.image = UIImage } にfuncのtableView(のtableView:のUITableView、didDeselectRowAtIndexPath indexPath:NSIndexPath) { せセル= tableView.cellForRowAtIndexPath(indexPath)など! RiskCustomCellVC cell.ivRiskCellImage.image = UIImage(名前が「UncheckedBox」) } – daydr3am3r

+0

ありがとうございました!私は知らないうちに同じミスをやろうとしていましたが、これは本当に意味があります! :) –

1

として脂肪私はあなたがイメージ形式を確認してチェックを外しなどを変更したいことを理解し、その逆

あり、主にテーブルビューのデリゲートの二つの方法

– tableView:didDeselectRowAtIndexPath: 
– tableView:didSelectRowAtIndexPath: 

参照リンク:https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewDelegate_Protocol/#//apple_ref/occ/intfm/UITableViewDelegate/tableView:didDeselectRowAtIndexPath

でも同じことができますが、tableView:didDeselectRowAtIndexPath:

func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) 
{ 
    print("tableView -> didSelectRowAtIndexPath") 

    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! CustomCellVC 

    cell.backgroundColor = UIColor.clearColor() 
    cell.ivRiskCellImage.image = UIImage(named: "**unCheckedBox**") 
} 
+0

ありがとうございます。私はすでにdidDeselectRowAtIndexPathを使ってみました。しかし、私のセルをMarkusとして指摘し、複数の選択を許可すると、それをdidSelectで選択してdidDelectで選択を解除することができたようです。 – daydr3am3r

関連する問題