UItableveiw的一些使用

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)



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值