Swift-UITableView, swift

Source: Internet
Author: User

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:

 

:

 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.