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!
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
    liudhkk阅读 12,979评论 3 38
  • #pragma mark someValueAboutTableView 1.tableView的样式:UITab...
    潇岩阅读 4,574评论 0 0
  • 这是一个奇妙的经历,
    难为水阅读 2,224评论 0 0
  • (一) 雨夜,灵魂安静的像个孤儿,走向远方。在尘封的记忆里,没有了下文。 我一个人待在房间里,周围是白色的墙壁,听...
    陳若心阅读 2,833评论 0 1
  • 小陈庄有一家人,姓杨,家有一子,起名曰杨意,意为洋洋得意,杨意自小被其父宠坏,要风得风,要雨得雨,在学校欺同学,骗...
    梅花映雪阅读 1,468评论 6 6

友情链接更多精彩内容