美文网首页iOS Developer
swift纯代码编写UITableView

swift纯代码编写UITableView

作者: 阿炎 | 来源:发表于2016-05-21 21:20 被阅读5739次

    本文主要使用纯代码方式创建UITableView视图,主要对一些常用方法进行了描述,有兴趣的朋友可以看看。

    ViewController中代码:

    1. 使用到UITableView的ViewController实现 UITableViewDelegate, UITableViewDataSource 协议
    class FindHouseViewController: UIViewController, UITableViewDelegate, UITableViewDataSource
    
    1. 初始化UITableView
    let houseTableView = UITableView()
    

    如果需要使用分组UITableView,需要在初始化的时候设置

    let tableView = UITableView(frame: CGRectMake(0, 0, 175, 500), style: .Grouped)
    
    1. 设置UITableView的属性
    // 去掉分割线
    houseTableView.separatorStyle = .None
    // 设置代理
    houseTableView.delegate = self
    // 设置数据源
    houseTableView.dataSource = self
    // 设置显示的内容顶点至UITableView边缘的偏移量,如下底部会留出15的空白间距
    houseTableView.contentInset = UIEdgeInsetsMake(0, 0, 15, 0)
    // 隐藏滚动条
    houseTableView.showsVerticalScrollIndicator = false
    // 设置背景色
    houseTableView.backgroundColor = UIColor(red: 0xef/255, green: 0xef/255, blue: 0xf4/255, alpha: 1)
    // cell预估高度
    houseTableView.estimatedRowHeight = 300
    // cell高度自适应
    houseTableView.rowHeight = UITableViewAutomaticDimension
    // 注册tableviewcell
    houseTableView.registerClass(HouseTableViewCell.self, forCellReuseIdentifier: cellIdentity)
    // 设置headerView
    houseTableView.headerView = XWRefreshNormalHeader(target: self, action: #selector(FindHouseViewController.refresh))
    // 设置footerView
    houseTableView.footerView = XWRefreshAutoNormalFooter(target: self, action: #selector(FindHouseViewController.loadMore))
    
    1. 将新建的tableview添加至当前视图,并设置约束,此处我使用SnapKit设置约束,感兴趣的朋友可以搜索下SnapKit的相关资料,在自动布局方面使用该框架非常便利。
    self.view.addSubview(houseTableView)
        houseTableView.snp_makeConstraints {(make) -> Void in
     // 充满整个屏幕
         make.top.left.bottom.right.equalTo(self.view)
    }
    
    1. 创建数据源,后面在实现协议的时候会用到
    var imageArray = ["http://192.168.0.111/111.jpg","http://192.168.0.111/222.jpg"]
    
    1. 实现UITableViewDataSource 协议中的两个方法,这两个方法必须实现
    // 每个节点返回的row的行数
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
         return imageArray.count
    }
    // 返回每行的cell的视图
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
         let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentity) as! HouseTableViewCell
         let placeHolderImg = UIImage(named: "placeholder.png")
         cell.houseImageView.kf_setImageWithURL(NSURL(string: imageArray[indexPath.item])!,
               placeholderImage: placeHolderImg!,
               optionsInfo: [.Transition(ImageTransition.Fade(1))])
         cell.houseLabel.text = "测试"
         cell.backgroundColor = UIColor(red: 0xef/255, green: 0xef/255, blue: 0xf4/255, alpha: 1)
         return cell
    }
    

    第二个方法中,indexParh有section和row属性,section代表是第几个节点,row属性代表该节点第几行,如果未设置UITableView一共有多少个节点,则默认为1个节点。
    如果需要实现cell按下高亮,手放开恢复原状态,则实现如下方法即可:

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
          tableView.deselectRowAtIndexPath(indexPath, animated: true)
    }
    

    至此,ViewController中关于UITableView的必要部分基本设置完毕,还有其它可实现的方法,各位朋友可以自行尝试。

    纯代码自定义UITableViewCell:
    1. 新建一个class继承至UITableViewCell,不用勾选创建xib文件选项,在新建的class中添加如下方法
    required init(coder aDecoder: NSCoder) {
         super.init(coder: aDecoder)!
    }
    
    1. 重写init方法,该方法必须实现
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
     super.init(style: style, reuseIdentifier: reuseIdentifier)
         // 设置选中cell时无高亮等效果
         self.selectionStyle = .None
    }
    

    至此,UITableViewCell代码编写完毕。
    ViewController与UITableViewCell的源代码我就不贴了,如果大家在使用中还有什么问题,可以私信我,我知道的会一一回答,希望本文对大家有所帮助,感谢大家的支持。

    相关文章

      网友评论

        本文标题:swift纯代码编写UITableView

        本文链接:https://www.haomeiwen.com/subject/gurorttx.html