快速創(chuàng)建Swift的TableView

1. 創(chuàng)建

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")

? ? }

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
禁止轉(zhuǎn)載,如需轉(zhuǎn)載請通過簡信或評論聯(lián)系作者。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容