最近在自学Swift,想要写一个自定义Cell,可是网上查了好多资料发现大家都是跟Xib结合使用的,本人对Xib不是特别擅长,所以想实现一款纯代码的自定义Cell
首先需要构建一个TableView
class MainViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var mainTable: UITableView?
var mainArray: NSMutableArray?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.view.backgroundColor = UIColor.whiteColor()
self.title = "下厨房"
self.setUpTable()
self.loadDataSource()
}
// MARK: - tableView Delegate && tableView DataSource
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 180
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print(mainArray!.count)
return mainArray!.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let identifier = "mainCell"
let cell = MainClassicalCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: identifier)
//下面两个属性对应subtitle
// cell.firstTitle?.text = mainArray![indexPath.row] as? String
// cell.subTitle?.text = mainArray![indexPath.row] as? String
var dic = Dictionary<String, String>()
dic["name"] = mainArray![indexPath.row] as? String
cell.setValueForCell(dic)
//添加照片
// cell.showImage?.image = UIImage(named: mainArray![indexPath.row] as! String)
return cell
}
func loadDataSource() {
mainArray = NSMutableArray.init(array: ["宝宝0", "宝宝1", "宝宝2", "宝宝3", "宝宝4", "宝宝5", "宝宝6", "宝宝7", "宝宝8", "宝宝9", "宝宝10", "宝宝11"])
self.mainTable?.reloadData()
}
func setUpTable(){
mainTable = UITableView.init(frame: CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT) , style: UITableViewStyle.Grouped)
mainTable!.delegate = self
mainTable!.dataSource = self
mainTable!.backgroundColor = UIColor.whiteColor()
self.view.addSubview(mainTable!)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
自定义Cell
import UIKit
class MainClassicalCell: UITableViewCell {
var showImage : UIImageView?
var firstTitle : UILabel?
var subTitle : UILabel?
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.setUpUI()
}
func setUpUI() {
showImage = UIImageView.init(frame: CGRectMake(5, 5, SCREENWIDTH-10, 170))
showImage!.layer.masksToBounds = true
self.addSubview(showImage!)
subTitle = UILabel.init(frame: CGRectMake(10, 150, (showImage?.frame.size.width)!-20, 12))
subTitle?.font = UIFont.systemFontOfSize(15)
subTitle?.textAlignment = NSTextAlignment.Center
subTitle?.textColor = UIColor.redColor()
self.addSubview(subTitle!)
firstTitle = UILabel.init(frame: CGRectMake(10, CGRectGetMinY((subTitle?.frame)!)-38, (subTitle?.frame.size.width)!, 30))
firstTitle?.font = UIFont.systemFontOfSize(18)
firstTitle?.textAlignment = NSTextAlignment.Center
firstTitle?.textColor = UIColor.blueColor()
self.addSubview(firstTitle!)
}
func setValueForCell(dic: NSDictionary) {
subTitle!.text = "45道菜谱"
firstTitle!.text = "世界各地大排档的招牌美食"
showImage!.image = UIImage(imageLiteral: "img.jpg")
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}