2016-10-19 5 views
0

テーブルビューのセルボタンを再利用したい。最初は、セルに2つのボタンが隠され、1つのボタンが表示されています。表示ボタンをクリックすると、隠れた2つのボタンも表示されます。ボタンターゲットアクションを設定しました。ここに私のコードはカスタムtableViewCellセルの非表示ボタンを再利用する[ios]

var count: Int = 0 
    func CounterBtnTapped(sender: UIButton) { 
      self.count += 1 
      let indexPath = IndexPath(row: sender.tag, section: 0)  
      let cell = self.tableView.dequeueReusableCell(withIdentifier: "CellForEndItem",for: indexPath) as! CellForEndItem 
      cell.incrementBtn.isHidden = false 
      cell.decreamentBtn.isHidden = false 
      sender.setTitle(String(self.count), for: .normal) 
    } 

しかし、どういうわけか...今すぐ怒鳴る私CounterBtnTapped方法がある

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "CellForEndItem", for: indexPath as IndexPath) as! CellForEndItem 
     cell.incrementBtn.isHidden = true 
     cell.decreamentBtn.isHidden = true 
     cell.priceLbl.text = "$ 200" 

     cell.counterBtn.tag = indexPath.row 
     cell.incrementBtn.tag = indexPath.row 
     cell.decreamentBtn.tag = indexPath.row 
     cell.counterBtn.addTarget(self, action: #selector(CounterBtnTapped(sender:)), for: .touchUpInside) } 

で、インクリメントとdecreamentボタンは表示されません。私はここで何をしているのですか。専門家の提案が必要ですか?

答えて

0

カスタムボタンを作成し、ケースのrightbarbuttonitemであるナビゲーションバーボタンにcustomViewを追加することができます。

var view = UIView(frame: CGRectMake(0, 0, 100, 44)) 
view.backgroundColor = UIColor.blueColor() 
var barButtonItem = UIBarButtonItem(customView: view) 
self.navigationItem.rightBarButtonItem = barButtonItem 

関連するコードを追加して、ボタンをデザインのように見せてから、それをcustomViewに追加します。

0

barButtonItemとしてカスタム表示を使用できます。

let customButton = UIButton(frame: .zero) 
// configure title text and color, background color, action, etc. using UIButton functions. 
// configure shadow, cornerRadius using customButton.layer properties 
navigationItem.rightBarButtonItem = UIBarButtonItem(customView: customButton) 
1

ストーリーボードを介してナビゲーションアイテムにボタンを置き、背景色を変更するだけです。 UIButtonの出口を作り、角を丸くします。

enter image description here

@IBOutlet weak var barBtn: UIButton! 

override func viewDidLoad() { 
    super.viewDidLoad() 


    barBtn.layer.cornerRadius = 5 
    barBtn.layer.borderWidth = 1 
    barBtn.layer.borderColor = UIColor.blackColor().CGColor 
} 
+0

その作業nice ...ありがとうlott .. –

+0

それを聞いて素晴らしい!他の人に役立つように答えを受け入れる:) – Sahil

+0

なぜ1か月後に受け入れられないのですか? – Sahil

0

問題はのtableViewセルをリフレッシュするためでした。私は、それは同様に動作

tableView.reloadRows(at: [indexPath], with: .automatic)

次のコード行を追加しました。