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! }

转载于:https://www.cnblogs.com/small-Sun/p/5667764.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值