Swift UITableView的基本用法

var tableview = UITableView()

//? ? 創(chuàng)建可變數(shù)組

var datas: NSMutableArray = ["1","2","3","4","5","6","7","8"]

override func viewDidLoad() {

super.viewDidLoad()

self.title = "UITableView"

//? ? ? ? 設(shè)置tableView的寬高

tableview = UITableView(frame: CGRectMake(0, 0, view.frame.size.width, view.frame.size.height),style:UITableViewStyle.Plain)

//? ? ? ? 設(shè)置代理

tableview.dataSource = self

//? ? ? ? 設(shè)置數(shù)據(jù)源

tableview.delegate = self

self.view.addSubview(tableview)

}

//? 代理方法tableView的行數(shù)

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{

//? ? 返回數(shù)組個數(shù)

return datas.count

}

//cell的獨復(fù)用機制

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{

let iden: String = "cell"

var cell = tableview.dequeueReusableCellWithIdentifier(iden)

if (cell == nil) {

cell = UITableViewCell(style: UITableViewCellStyle.Value1,reuseIdentifier: iden)

}

cell?.textLabel?.text = self.datas[indexPath.row] as? String

return cell!

}

//? ? 設(shè)置cell的高度

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat{

return 80

}

//? ? 左滑動的時候自定義兩個按鈕

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {

//? ? ? ? 刪除cell

let deleteAct : UITableViewRowAction = UITableViewRowAction(style: .Default,title: "刪除"){(UITableViewRowAction,NSIndexPath)in

print("刪除")

self.datas.removeObjectAtIndex(NSIndexPath.row)

tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)//平滑效果

//? ? ? ? ? ? 刷線表單

tableView.reloadData()

}

let likeAct : UITableViewRowAction = UITableViewRowAction(style: .Normal,title: "喜歡"){ (UITableViewRowAction, NSIndexPath) in

print("喜歡")

}

return [deleteAct,likeAct]

}

最后編輯于
?著作權(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ù)。

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

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