One of IOS development cell customization methods
The first method is to directly write an inherited UITableViewCell. Then add the subview dynamically.
Procedure:
1. Create a new cell class that inherits UITableViewCell
2. Define attributes in this class, such as UILabel
3. Reload the constructor to add the Sub-View
4. You can use this class.
CityCellTableViewCell class:
/// CityCellTableViewCell. swift // UITableViewDemo0 /// Created by Wang Feng on 7/27/15. // Copyright (c) 2015 wangfeng. all rights reserved. // import UIKitclass CityCellTableViewCell: UITableViewCell {var cityLabel: UILabel? Var cityTextFiled: UITextField? Var citySwitch: UISwitch? Override init (style: UITableViewCellStyle, reuseIdentifier: String ?) {Super. init (style: style, reuseIdentifier: reuseIdentifier) // initialize the sub-view and sub-control, and then add it to the current view cityLabel = UILabel (frame: CGRect (x: 5, y: 5, width: 80, height: 40) cityTextFiled = UITextField (frame: CGRect (x: 90, y: 5, width: 80, height: 40) citySwitch = UISwitch (frame: CGRect (x: 200, y: 5, width: 80, height: 40) // Add to current view self. addSubview (cityLabel !) Self. addSubview (cityTextFiled !) Self. addSubview (citySwitch !)} Required init (coder aDecoder: NSCoder) {fatalError (init (coder :) has not been implemented)} 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 }}
ViewController reload tableView method:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{ var cellId = mycell var cell:CityCellTableViewCell? = (tableView.dequeueReusableCellWithIdentifier(cellId) as? CityCellTableViewCell) if(cell == nil){ cell = CityCellTableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: cellId) } cell?.cityLabel?.text = cities[indexPath.row] cell?.cityTextFiled?.placeholder = input number cell?.citySwitch?.on = true return cell! }