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...
    炙冰阅读 7,315评论 1 14
  • 解决添加到ScrollView上的UITableView控件自动向下偏移64像素的问题 首先理解1:即使UITab...
    CoderZb阅读 10,693评论 1 8
  • 废话不多说,直接上干货 ---------------------------------------------...
    小小赵纸农阅读 8,783评论 0 15
  • 楼下的菜场随着近几年的发展渐成规模,天亮开至午后即收工散去。凌晨五点左右也有上班族匆匆赶来买完旋即归去。他们让我想...
    花园里的皮皮阅读 1,423评论 0 0
  • 生于乡村,长于山野。 土坡,大大小小,栽满了我彩色的记忆;沟壑,深深浅浅,掩藏着我无尽的怀念。 站在祖辈们的破屋残...
    落筆轩阅读 981评论 0 0