Basic use of swift-UITableView, swift-uitableview

Source: Internet
Author: User

Basic use of swift-UITableView, swift-uitableview

If you don't talk much about it, paste the code I wrote today: If you don't understand anything, you can send me an email.

//

// SingleInfo. swift personal information

// Housekeeper

//

// Created by Lu Yang on 15/10/27.

// Copyright©Nevenmoore. All rights reserved.

//

 

Import Foundation

Import UIKit

Class singleInfo: UIViewController, UITableViewDataSource, UITableViewDelegate {

Var dataTable: UITableView !; // Data table

Var itemString = ["nickname", "Account", "gender", "region", "My Car"]

// Current Screen Object

Var screenObject = UIScreen. mainScreen (). bounds;

// Page Initialization

Override func viewDidLoad (){

Super. viewDidLoad ();

InitView ();

}

/**

UI Initialization

*/

Func initView (){

Self. title = "My Documents ";

Self. view. backgroundColor = UIColor. linghtGreyBg ();

CreatTable ();

}

/**

My data table Initialization

*/

Func creatTable (){

Let dataTableW: CGFloat = screenObject. width;

Let dataTableH: CGFloat = screenObject. height;

Let dataTableX: CGFloat = 0;

Let dataTableY: CGFloat = 0;

DataTable = UITableView (frame: CGRectMake (dataTableX, dataTableY, dataTableW, ableableh), style: UITableViewStyle. Grouped );

DataTable. delegate = self; // implement proxy

DataTable. dataSource = self; // implement the data source

Self. view. addSubview (dataTable );

}

// 1.1 A group is returned by default

Func numberOfSectionsInTableView (tableView: UITableView)-> Int {

Return 2;

}

// 1.2 rows returned

Func tableView (tableView: UITableView, numberOfRowsInSection section: Int)-> Int {

If (section = 0 ){

Return 1;

} Else {

Return 5;

}

}

// 1.3 returns the Row Height

Func tableView (tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath)-> CGFloat {

If (indexPath. section = 0 ){

Return 80;

} Else {

Return 55;

}

}

// 1.4 head height of each group

Func tableView (tableView: UITableView, heightForHeaderInSection section: Int)-> CGFloat {

Return 10;

}

// 1.5 bottom height of each group

Func tableView (tableView: UITableView, heightForFooterInSection section: Int)-> CGFloat {

Return 1;

}

// 1.6 Data Source returned

Func tableView (tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)-> UITableViewCell {

Let identifier = "identtifier ";

Var cell = tableView. dequeueReusableCellWithIdentifier (identifier );

If (cell = nil ){

Cell = UITableViewCell (style: UITableViewCellStyle. Value1, reuseIdentifier: identifier );

}

If (indexPath. section = 0 ){

Cell ?. TextLabel ?. Text = "Avatar ";

} Else {

Cell ?. TextLabel ?. Text = itemString [indexPath. row];

}

Cell ?. AccessoryType = UITableViewCellAccessoryType. DisclosureIndicator;

Return cell !;

}

// 1.7 table Click Event

Func tableView (tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath ){

// Cancel the selected Style

TableView. deselectRowAtIndexPath (indexPath, animated: true );

// Obtain the index of the row to be clicked

If (indexPath. row = 0 ){

Let pushSingleInfo = singleInfo ();

PushSingleInfo. hidesBottomBarWhenPushed = true; // hide the navigation bar

Self. navigationController ?. PushViewController (pushSingleInfo, animated: true );

}

}

 

// Memory warning

Override func didReceiveMemoryWarning (){

Super. didReceiveMemoryWarning ();

Print ("personal information memory warning ");

}

}

As follows:

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.