UItableveiw的一些使用

来源:互联网 发布:苏亚雷斯巴萨数据 编辑:程序博客网 时间:2024/05/16 02:18

UItableview是iOS用来展示数据的一种重要控件,几乎所有的数据展示页面都可以用UItableView来做

UItableview使用首先要定义声明,可用代码定义也可用storybord直接拖,要设置代理和数据源  

代理模式是iOS开发中的重要模式,很好的使用代理可以让代码写起来更省力,有些代理的使用也可以用swift中的闭包或object-c中的block来替代,之后我会上一个代理的例子

tv.delegate = self;

tv.dataSource = self;
tableView使用时一定要明白其中的每个代理方法:

常用的代理方法有

定义section数量:

    func numberOfSections(in tableView: UITableView) -> Int {        return 1    }
定义row数量
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {        return 10    }
定义cell
 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {        let cell = tableView.dequeueReusableCell(withIdentifier: "ServerCell") as! ServerCell      //自定义tableviewcell 复用return cell}

如果不需要自定义cell,也可用系统提供的4种cell

let cell = UITableViewCell(style: .value1, reuseIdentifier: "cel")

4种cell有

public enum UITableViewCellStyle : Int {        case `default` // Simple cell with text label and optional image view (behavior of UITableViewCell in iPhoneOS 2.x)    case value1 // Left aligned label on left and right aligned label on right with blue text (Used in Settings)    case value2 // Right aligned label on left with blue text and left aligned label on right (Used in Phone/Contacts)    case subtitle // Left aligned label on top and left aligned label on bottom with gray text (Used in iPod).}

每行的行高:

  func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {        return 10    }

一些常用代码:

UItableViewCell加箭头:

cell.accessoryType = .disclosureIndicator

去除系统的分割线:

tv.separatorStyle = .none

tableViewCell 的选中效果:

cell.selectionStyle = .none

如果希望tableview刷新的时候有动画效果
刷新动画//        let xx : NSIndexSet = NSIndexSet(index: section)//        self.tableveiw.reloadSections(xx as IndexSet, with:.top)



1 0
原创粉丝点击