iOS Developer

OC转swift3.0 实践 (二)使用自定义cell的tabl

2016-12-05  本文已影响904人  APP叫我取个帅气的昵称

写在前面的话:
先看下效果图,如下:

使用自定义cell的tableview.png
本来此次是不打算写的,因为太简单了,但后来想想刚学就要稳(fan)扎(zheng)稳(xian)打(zhe),在这来记一下吧。至此为止这些图片都是直接一个URL,网络层的东西日后会去慢慢探究。(听说转JSON特别蛋疼)
既然图片是一个URL,那就少不了SDWebImage这个OC库了,就又少不了BridgingHeader文件了,简单说一下吧:
command +N ——>选择Header File——>命名确定——>然后在新建的.h文件里像OC一样 #import <你想要的库(cocoapods已经先导入)>,如下图1——>最后一步,也是最重要的一步,在building setting中搜索swift Compiler,如图二,将桥文件的路径拖进去才能生效 图1.jpeg
图2.jpeg

好了 下面说下controller里要写的代码(自定义的cell和OC没啥差哦)代码如下

import UIKit

//mark: 注册自定义的cellID
fileprivate let ZLMSaleCellID = "ZLMSaleTableViewCell"

class ZLMSaleViewController: UIViewController {

    lazy var tableview :UITableView = {
        self.automaticallyAdjustsScrollViewInsets = false
        let tab = UITableView(frame:CGRect(x:0,y:64,width:self.view.frame.size.width,height:self.view.frame.size.height-64-49),style:.grouped)
        tab.delegate = self
        tab.dataSource = self
        tab.separatorStyle = .none
        tab.showsVerticalScrollIndicator = false
        ///注册CellID
        tab.register(UINib(nibName:ZLMSaleCellID,bundle:nil),
        forCellReuseIdentifier:ZLMSaleCellID)
        
       return tab
    }()
    let imagesArrayUrlstring = ["http://fdfs.xmcdn.com/group24/M06/8F/D5/wKgJNVhDe8zjowOnAARzULS5SD4646_ios_large.jpg","http://fdfs.xmcdn.com/group21/M0A/89/FE/wKgJLVhBPhji87KEAAHhNNU_Acc906_ios_large.jpg","http://fdfs.xmcdn.com/group24/M08/8F/CE/wKgJNVhDebbBfm1mAAHNi4F_D9U325_ios_large.jpg","http://fdfs.xmcdn.com/group24/M09/8E/CB/wKgJMFhC3JSB2W99AAUl8MmV0m4830_ios_large.jpg","http://fdfs.xmcdn.com/group25/M05/8E/B0/wKgJNlhC2gfCUAhOAAGtp6hEjoE035_ios_large.jpg"];
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.white
        self.automaticallyAdjustsScrollViewInsets = true
        self.view.addSubview(tableview)
        
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
}
extension ZLMSaleViewController:UITableViewDelegate,UITableViewDataSource{
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return imagesArrayUrlstring.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: ZLMSaleCellID)as?ZLMSaleTableViewCell
        cell?.saleImageV.sd_setImage(with:URL(string:imagesArrayUrlstring[indexPath.row]))
        return cell!
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 130
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0.01
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        return
    }
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.01
    }
    
}
上一篇下一篇

猜你喜欢

热点阅读