iOS - UITableView 总结

2016-07-12  本文已影响96人  墨迹momo

UITableView是iOS开发中常用到的控件,UITableView
继承自UIScrollView,很强大,多用于数据的显示。

1. 创建tableView

let tableView = UITableView(frame: CGRect(x: 0, y: 0, 100, 200))
tableView.delegate = self 
tableView.dataSource = self
tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "tableView")
self.view.addSubview(tableView)

在VC类后面添加 UITableViewDataSource, UITableViewDataSource
Tips:最好直接创建UIViewControl然后添加UITableView,而非直接使用UITableViewControl。直接使用UITableViewControl会使得self.view就是tableView本身,从而造成一些功能的不方便,例如添加一个悬浮按钮。

2. 配置数据

a.配置行数

///确定行数
func tableView(tv:UITableView, numberOfRowsInSection section:Int) -> Int {
     return 10
}

b.配置cell数据:

func tableView(tv:UITableView, cellForRowAtIndexPath indexPath:NSIndexPath) -> UITableViewCell {        
     //故事版cell
     //let cell = tableView.dequeueReusableCellWithIdentifier(Storyboard.CellReusIdentifier, forIndexPath: indexPath) as! ProcessTableViewCell 
     //自创cell
     let cell = tableView .dequeueReusableCellWithIdentifier("tableView") 
     //配置cell数据
     cell.process = processes[index]
     return cell
 } 

c.确认节数

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
     return months.count    
 }

d. 第section分区的头部标题

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
     return months[section]
 }

e.第section分区的底部标题

func tableView(tableView: UITableView, titleForFooterInSection section: Int) -> String? {
     return months[section]
 }

3. 编辑模式

首先要删除数据源,然后删除那一行。
a.开启编辑模式

self.processTableView.setEditing(true, animated: true)

b.是否每一行能够编辑 如果返回是,向右滑动会显示删除按钮

func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
     return true
}

c.删除某一行

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
     var index = 0
     for var group = 0; group < indexPath.section ; group++ {
         index += records[group]
     }
     index += indexPath.row
     //删除数据库
     let process =  processes[index]
     process.deleteProcess()
     //删除数据源
     processes.removeAtIndex(index)
     records[indexPath.section]--
     //是否是分组最后一行
     if records[indexPath.section] == 0{
         records.removeAtIndex(indexPath.section)
         months.removeAtIndex(indexPath.section)
          //删除某分组
         tableView.deleteSections(NSIndexSet(index: indexPath.section), withRowAnimation:  .Fade)
     }else{
         //删除某行
         tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
     }
}

4. 设置参数

a.设置表头高1像素

self.tableView.tableHeaderView = UIView(frame: CGRectMake(0, 0, 0, 1))

b.设置表尾高1像素

self.tableView.tableFooterView = UIView(frame: CGRectMake(0, 0, 0, 1))

c.设置section头高1像素

self.tableView.sectionFooterHeight = 1

d.设置section尾高1像素

self.tableView.sectionHeaderHeight = 1

e.表格行高

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

5. 设置自适应高度

self.tableView.estimatedRowHeight = 44.0
self.tableView.rowHeight = UITableViewAutomaticDimension

6.点击触发

通过一开始为不同表格设置不同tag来区分表格

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
     //表格类型
     switch tableView.tag {
         //菜单表格
     case tableViewTag.MuneTable:
         if indexPath.row == 1 {
             self.projectTableView.reloadData()
         }else if indexPath.row == 2{
             handleCallOptions()
         }
     case tableViewTag.ProjectsTable:
         return
     default:
         return
     }
}
上一篇 下一篇

猜你喜欢

热点阅读