Swift-UITableView, swift
Custom a controller CustomViewController
AppDelegate (convenient without importing header files)
Func application (application: UIApplication, didfinishlaunchingwitexceptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Initialize the custom Controller
Var vc = CustomViewController ()
// Add a navigation Controller
Var nav = UINavigationController (rootViewController: vc)
// Set the initialization Window to the screen size
Self. window = UIWindow (frame: UIScreen. mainScreen (). bounds)
Self. window ?. RootViewController = nav
Self. window ?. MakeKeyAndVisible ()
Return true
}
Customize a UITableViewCell
Import UIKit
Class TestTableViewCell: UITableViewCell {
Var leftImageView: UIImageView!
Var label: UILabel!
Override init (style: UITableViewCellStyle, reuseIdentifier: String ?) {
Super. init (style: style, reuseIdentifier: reuseIdentifier)
// Initialization
Self. leftImageView = UIImageView (frame: CGRectMake (0, 0,100, 80 ))
Self. leftImageView. backgroundColor = UIColor. redColor ()
Var leftWidth = CGRectGetMaxX (self. leftImageView. frame)
Var left = CGRectGetMaxX (self. leftImageView. frame)
Self. label = UILabel (frame: CGRectMake (CGRectGetMaxX (self. leftImageView. frame) + 10, 0,100, 30 ))
Self. leftImageView. image = UIImage (named: "cell_img.jpg ")
Self. label. text = "text"
Self. contentView. addSubview (leftImageView)
Self. contentView. addSubview (label)
}
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
}
}
Customize a UITableView
Import UIKit
Class TestTableView: UITableView, UITableViewDataSource, UITableViewDelegate {
Override init (frame: CGRect, style: UITableViewStyle ){
Super. init (frame: frame, style: style)
Self. initView ()
}
Required init (coder aDecoder: NSCoder ){
FatalError ("init (coder :) has not been implemented ")
}
Func initView (){
Self. dataSource = self
Self. delegate = self
Self. registerClass (TestTableViewCell. self, forCellReuseIdentifier: "cellID ")
}
// MARK:-UITableViewDataSource, UITableViewDelegate proxy method
Func tableView (tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)-> UITableViewCell {
// Reuse cell
Var cell = tableView. dequeueReusableCellWithIdentifier ("cellID")! TestTableViewCell
// Cell. selectionStyle = UITableViewCellSelectionStyle. None
Return cell
}
Func tableView (tableView: UITableView, numberOfRowsInSection section: Int)-> Int {
// Return quantity
Return 10
}
Func tableView (tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath)-> CGFloat {
Return 100
}
Func tableView (tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath ){
// Cell click Effect
TableView. deselectRowAtIndexPath (indexPath, animated: true)
}
}
Initialize the custom UITableView and add it to the view.
Import UIKit
// Uninstall the agent.
//, UITableViewDataSource, UITableViewDelegate
Class CustomViewController: UIViewController {
// Declare UITableView global variables
Var tableView: TestTableView!
Override func viewDidLoad (){
Super. viewDidLoad ()
Self. view. backgroundColor = UIColor. redColor ()
// Use UITableViewStyle. Plain for enumeration.
Self. tableView = TestTableView (frame: self. view. bounds, style: UITableViewStyle. Plain)
Self. view. addSubview (self. tableView)
}
}
Directory:
: