快速创建Swift的TableView

1. 创建

lazy var tableView : UITableView = {

        lettableView =UITableView.init(frame:.zero,style:UITableView.Style.plain)

        tableView.separatorStyle = UITableViewCell.SeparatorStyle.singleLine

        tableView.backgroundColor="F3F6F9".uicolor()

        tableView.separatorInset=UIEdgeInsets.init(top:0,left:0,bottom:0,right:0)

        tableView.estimatedRowHeight=0

        tableView.estimatedSectionFooterHeight = 0

        tableView.estimatedSectionHeaderHeight = 0

        tableView.dataSource=self

        tableView.delegate=self

        tableView.register(MyCell.self, forCellReuseIdentifier: "mycell")

        tableView.tableFooterView=UIView()

        // 下拉刷新

        tableView.es.addPullToRefresh(animator: header, handler: {

            [unownedself]in

            self.headerRereshing()

        });

        // 上拉加载

        tableView.es.addInfiniteScrolling(animator: footer) {

            [unownedself]in

            self.footerRereshing()

        }

        returntableView

    }()


2.事件

  /// 下拉刷新

    func headerRereshing() {

        UIView .performWithoutAnimation {

            self.tableView.es.stopPullToRefresh()

        }

    }


    ///上拉加载

    func footerRereshing() {

        UIView.performWithoutAnimation {

            self.tableView.es.stopLoadingMore()

        }

    }


extension ViewController: UITableViewDelegate,UITableViewDataSource {

    functableView(_tableView:UITableView,numberOfRowsInSectionsection:Int) ->Int{

        return 8

    }


    functableView(_tableView:UITableView,cellForRowAtindexPath:IndexPath) ->UITableViewCell{

        let cell:MyTableViewCell! = tableView.dequeueReusableCell(withIdentifier: "mycell",for: indexPath) as?MyTableViewCell

        returncell

    }


    functableView(_tableView:UITableView,heightForRowAtindexPath:IndexPath) ->CGFloat{

        return156.0

    }


}


class MyTableViewCell: UITableViewCell {

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {

        super.init(style: .default,reuseIdentifier: reuseIdentifier)

        self.selectionStyle = UITableViewCell.SelectionStyle.none

           }


    requiredinit?(coder:NSCoder) {

        fatalError("init(coder:) has not been implemented")

    }

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
禁止转载,如需转载请通过简信或评论联系作者。

推荐阅读更多精彩内容