Swift UITableView的基本用法

var tableview = UITableView()

//    创建可变数组

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

override func viewDidLoad() {

super.viewDidLoad()

self.title = "UITableView"

//        设置tableView的宽高

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

//        设置代理

tableview.dataSource = self

//        设置数据源

tableview.delegate = self

self.view.addSubview(tableview)

}

//  代理方法tableView的行数

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

//    返回数组个数

return datas.count

}

//cell的独复用机制

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!

}

//    设置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]

}

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

推荐阅读更多精彩内容

  • 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
    liudhkk阅读 9,118评论 3 38
  • 1.nav1.navigationBar.barStyle=UIBarStyleBlack; //改变导航栏背景颜...
    SadMine阅读 1,692评论 1 4
  • 自定义单元格 表格无论有多少中自定义单元格样式 每一种自定义单元格都有复用的能力所以每一个单元格都要带有一个静态局...
    DVWang阅读 288评论 0 0
  • import UIKit class ViewController: UIViewController ,UITa...
    小_蜡笔阅读 1,276评论 0 1
  • 明明很想要 明明在祈祷 明明想到爆 明明忘不了 你在怕什么 温情后的日日难熬 久待时的突然疯掉 离开后的想要打扰 ...
    子曰0215阅读 174评论 0 0