Swift UI 21 利用UITableView實現瀑布流效應

1:瀑布流效應一般使用UICollectionView(網格)實現
2:利用UITableView的父類是UIScrollView也可以實現
3:父類中的協議方法 (協議中的方法是可以繼承的)
4:在屏幕兩端各設置兩個tableView(寬為屏寬的一半)
5:利用父協議中的方法func scrollViewDidScroll(scrollView: UIScrollView)
6:控制兩個tableView實現聯動效果(設置contentOffset相等)

具體代碼如下:

    let width = UIScreen.mainScreen().bounds.size.width
    let height = UIScreen.mainScreen().bounds.size.height
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        createUI()
    }

    func createUI(){
        let tableView1 = UITableView.init(frame: CGRectMake(0, 0, width / 2, height))
        let tableView2 = UITableView.init(frame: CGRectMake(width / 2, 0, width / 2, height))
        
        tableView1.showsVerticalScrollIndicator = false
        
        tableView1.tag = 10
        tableView2.tag = 20
        
        self.view.addSubview(tableView1)
        self.view.addSubview(tableView2)
        
        tableView1.delegate = self
        tableView1.dataSource = self
        
        tableView2.dataSource = self
        tableView2.delegate = self
    }
    
    //協議中的方法
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if tableView.tag == 10 {
            return 30
        } else {
            return 40
        }
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        if tableView.tag == 10 {
            var cell = tableView.dequeueReusableCellWithIdentifier("LWY")
            if cell == nil {
                cell = UITableViewCell.init(style: .Default, reuseIdentifier: "LWY")
            }
            cell?.textLabel?.text = "\(indexPath.row)"
            return cell!
       
        } else {
            var cell = tableView.dequeueReusableCellWithIdentifier("HY")
            if cell == nil {
                cell = UITableViewCell.init(style: .Default, reuseIdentifier: "HY")
            }
            cell?.textLabel?.text = "Hello, world"
            return cell!
        }
        
    }
    
    //設置行高(默認行高是44)
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return CGFloat(arc4random() % 61) + 40
    }
    
    
    //父類中的協議方法 (協議中的方法是可以繼承的)
    func scrollViewDidScroll(scrollView: UIScrollView) {
        print(scrollView.tag)
        
        let tableView1 = self.view.viewWithTag(10) as! UITableView
        let tableView2 = self.view.viewWithTag(20) as! UITableView
        
        //如果滾動左側視圖時,則設置右側視圖的偏移量等于左側視圖的偏移量
        if scrollView == tableView1 {
            tableView2.contentOffset = tableView1.contentOffset
        } else {
            tableView1.contentOffset = tableView2.contentOffset
        }
    }
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • *7月8日上午 N:Block :跟一個函數塊差不多,會對里面所有的內容的引用計數+1,想要解決就用__block...
    炙冰閱讀 2,546評論 1 14
  • 解決添加到ScrollView上的UITableView控件自動向下偏移64像素的問題 首先理解1:即使UITab...
    CoderZb閱讀 5,285評論 1 8
  • 廢話不多說,直接上干貨 ---------------------------------------------...
    小小趙紙農閱讀 3,436評論 0 15
  • 樓下的菜場隨著近幾年的發展漸成規模,天亮開至午后即收工散去。凌晨五點左右也有上班族匆匆趕來買完旋即歸去。他們讓我想...
    花園里的皮皮閱讀 210評論 0 0
  • 生于鄉村,長于山野。 土坡,大大小小,栽滿了我彩色的記憶;溝壑,深深淺淺,掩藏著我無盡的懷念。 站在祖輩們的破屋殘...
    落筆軒閱讀 146評論 0 0