Swift中纯代码创建tableViewCell

import UIKit

class ViewController: UITableViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    //设置数据源和代理
    tableView.dataSource = self
    tableView.delegate = self

    //设置重用ID
    tableView.registerClass(UITableViewCell.classForCoder(), forCellReuseIdentifier: "cell")
}

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return 80
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 5

}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 4
}

//代理设置cell
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) 

    cell.backgroundColor = UIColor.blueColor()
    cell.textLabel?.text = "hello\(indexPath.row)"

    //设置cell的一些属性······
    return cell
}

}

或参照下面代码

import UIKit

class MineCenterCell: UITableViewCell {

var TitleString:String?
var iconImageName:String?

var TitleLabel:UILabel?
var iconImageView:UIImageView?


override init(style: UITableViewCellStyle, reuseIdentifier: String?) {

    super.init(style: style, reuseIdentifier: reuseIdentifier)


    self.iconImageView=UIImageView()

    self.contentView.addSubview(self.iconImageView!)

    self.TitleLabel=UILabel()

    self.contentView.addSubview(self.TitleLabel!)

    setUpviews()

}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}




func setUpviews() {

    if self.iconImageName != nil {

        self.iconImageView?.image=UIImage(named: iconImageName!)

        self.TitleLabel?.text=self.TitleString

    }

    self.iconImageView?.snp_makeConstraints(closure: { (make) in

        make.top.equalTo(10)
        make.bottom.equalTo(-10)
        make.left.equalTo(5)
        make.width.equalTo(self.iconImageView!.snp_height)

    })


    self.TitleLabel?.snp_makeConstraints(closure: { (make) in

        make.left.equalTo(self.iconImageView!.snp_right).offset(10)
        make.centerY.equalTo(self.iconImageView!.snp_centerY)

    })

}


override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}


override func layoutSubviews() {
    super.layoutSubviews()

    setUpviews()

}


override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
if indexPath.section == 0 {
let cell = MineInfoCell.cellWithTableView(tableView)
cell.delegate=self
return cell
}

    let indentifier = "MineCenterCell"

    var cell:MineCenterCell! = tableView.dequeueReusableCellWithIdentifier(indentifier) as? MineCenterCell

    if cell == nil {

        cell=MineCenterCell(style: .Default, reuseIdentifier: indentifier)
    }

    let images = [["ji-fen"],["ZB建议书","ZB投保单","ZB自修营"],["ZB团队管理","ZB业绩管理","ZB考勤"],["ZBAPPShare","ZBSetting"]]

    let titles = [["我的积分:\(USERINFO.sharedInstance.getpoint())"],["我的建议书","我的投保单","自修营"],["团队管理","业绩管理","我的考勤"],["分享App","设置"]]

    if indexPath.section == 1 {
        cell?.accessoryType = .DisclosureIndicator
    }

     cell?.iconImageName=images[indexPath.section - 1][indexPath.row]

     cell.TitleString=titles[indexPath.section - 1][indexPath.row]

    return cell!
}
posted @ 2016-07-13 17:46  small_Sun  阅读(11569)  评论(0编辑  收藏  举报