swift-UITableView 的使用,XIB 自定义Cell

swift的UITableView的用法和 OC 一样,只是有些语法上的差别。
// iOS 8自动行高估算
        tableView.estimatedRowHeight = 100
       tableView.rowHeight =UITableViewAutomaticDimension
1、首先使用纯代码编写UITableView
在 VC 中
letCellID_1 ="CellID_1" //1.cellID 常量
//1.创建UITableView(OC几乎一样)
        let ihgTableView = UITableView(frame: view.bounds, style:UITableViewStyle.Plain)
       
//2.注册Cell  id建议使用常量
        ihgTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: CellID_1)
       
//下面这种写法也是可以的
        //tableView.registerClass(UITableViewCell.classForCoder(), forCellReuseIdentifier: ID)
        //3.设置数据源和代理
        ihgTableView.dataSource=self
        ihgTableView.
delegate=self;
       
//4.添加到view
        self.view.addSubview(ihgTableView)
//MARK:----------设置TableView
    func numberOfSectionsInTableView(tableView:UITableView) ->Int{
       
return3;
    }
   
func tableView(tableView: UITableView, numberOfRowsInSection section:Int) ->Int{
       
return5
    }
   
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath:NSIndexPath) ->CGFloat{
       
return60
    }
   
func tableView(tableView: UITableView, heightForHeaderInSection section:Int) ->CGFloat{
       
return20
    }
   
func tableView(tableView: UITableView, heightForFooterInSection section:Int) ->CGFloat{
       
return10
    }
   
func tableView(tableView: UITableView, viewForHeaderInSection section:Int) ->UIView? {
       
let view = UIView()
        view.
backgroundColor=UIColor.redColor()
       
returnview
    }
   
func tableView(tableView: UITableView, viewForFooterInSection section:Int) ->UIView? {
       
returnnil
    }
   
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath:NSIndexPath) ->UITableViewCell{
       
let xiaofeiCell = tableView.dequeueReusableCellWithIdentifier(CellID_1, forIndexPath: indexPath)
        xiaofeiCell.
textLabel?.text="\(indexPath.section)组、第\(indexPath.row)"
       
returnxiaofeiCell
       
    }
   
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath:NSIndexPath) {
       
print("点击第\(indexPath.section)组、第\(indexPath.row)")
    }

XIB 创建 Cell  
个人比较钟情于在创建类的时候勾选 Also create XIB file 选项,因为个人觉得这样创建省去了后期的一些麻烦。但是不管是使用 storyboard 还是后面重新添加的 XIB 。一定不要忘了各种关联项。
废话不说直接贴代码
 @IBOutlet weak var ihgTableView: UITableView!
let HuanGouCell_1 = "HuanGouGoodsCell" //1.cell的ID,
//2.注册Cell  id建议使用常量
ihgTableView.registerNib(UINib(nibName: "HuanGouGoodsCell", bundle: nil), forCellReuseIdentifier: HuanGouCell_1)
//— — 这里的 cell 是单独的一个 XIB 一定要用 registerNib 的注册方式 当然如果不是使用拖控件的方式还是用 registerClass 的方式  ihgTableView.registerClass(HuanGouGoodsCell.self, forCellReuseIdentifier: HuanGouCell_1)
        //3.设置数据源和代理
ihgTableView.dataSource = self
ihgTableView.delegate = self;
//MARK:---------- 设置 TableView
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 20
    }
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
       
        let hgCell = tableView.dequeueReusableCellWithIdentifier(HuanGouCell_1, forIndexPath: indexPath) as! HuanGouGoodsCell
        hgCell.setCellWithIndx(indexPath)
        return hgCell
}
@IBOutlet weak var titleLabel: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()
        
    }

    func setCellWithIndx(indexPath:NSIndexPath) {
        titleLabel.text = "第\(indexPath.row)行"
    }





在 storyboard 里面使用。这种方法也是最简便了。
let idBookCell = "BookCell"
@IBOutletweakvar tableView: UITableView!
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        returnbooks.count
    }
   
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let bookCell = tableView.dequeueReusableCellWithIdentifier(idBookCell, forIndexPath: indexPath) as! BookCell
        bookCell.configureWithBook(indexPath)
        return bookCell
 }

func configureWithBook(indexPath:NSIndexPath) {
}


你可能感兴趣的:(swift-UITableView 的使用,XIB 自定义Cell)