2017-10-28 169 views
-1

目的如何共享多個UITableView的細胞

之間相同的UIView當任何3個藍色按鈕的用戶點擊,所有按鈕更改爲相同的顏色。

注:這是一個共享的發展觀問題的抽象,這是很重要的,只有一個UIView的是在我的三排共享(或模仿)

這裏是一個編譯斯威夫特項目:

import UIKit 

class ToggleButton: UIButton { 
    var connectedView: UIView? 
    func onPress() { 
     self.isHidden = true 
     self.connectedView?.isHidden = false 
    } 
} 

class ViewController : UIViewController, UITableViewDelegate, UITableViewDataSource { 

    var tableView: UITableView = UITableView(frame: CGRect(x: 0, y: 0, width: 300, height: 300)) 

    var myView: UIView? = nil 
    var toggleBtn: ToggleButton? = nil 

    override func viewDidLoad() { 
     self.setupTableView() 
    } 

    fileprivate func setupTableView() { 
     self.tableView.dataSource = self 
     self.tableView.delegate = self 

     self.tableView.backgroundColor = UIColor.white 
     self.tableView.isOpaque = true 

     self.view.addSubview(self.tableView) 
    } 

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = UITableViewCell(style: .default, reuseIdentifier: "CellIdentifier") 

     let frame = CGRect(x: 10, y: 10, width: 30, height: 30) 

     if let view = self.myView, let btn = self.toggleBtn { 
      cell.addSubview(view) 
      cell.addSubview(btn) 
     } else { 
      let myView = UIView(frame: frame) 
      myView.backgroundColor = UIColor.green 
      myView.isHidden = true 

      cell.addSubview(myView) 

      let toggleBtn = ToggleButton(frame: frame) 
      toggleBtn.backgroundColor = UIColor.blue 
      toggleBtn.addTarget(self, action: #selector(onPress), for: .touchUpInside) 
      toggleBtn.connectedView = myView 

      cell.addSubview(toggleBtn) 
     } 

     return cell 
    } 

    @objc func onPress(_ sender: Any) { 
     if let button = sender as? ToggleButton { 
      button.onPress() 
     } 
    } 
} 

任何幫助表示讚賞。

回答

0

UITableViewCell概念被製成非常彼此獨立。

所以你唯一可以做在你的視圖控制器一個布爾標誌,那麼你初始化你的3個單元與此標誌。

最後每按一次按鈕,你切換標誌EN重新加載您的tableView。