Swift中純代碼建立tableViewCell

import UIKitswift

class ViewController: UITableViewController {ide

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 }

}ui

或參照下面代碼spa

import UIKit代理

class MineCenterCell: UITableViewCell {code

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 }

}get

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

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! }
相關文章
相關標籤/搜索