A highly adaptive example of tableview cell in Swift

Source: Internet
Author: User
1, Effect chart
There are two text labels in the cell, showing a description of the title and the contents respectively. At the same time, two labels are self growing.

2,storyboard settings
(1) Set the cell identifier to "MyCell"

(2) Drag a 2 Label control from the object library into the cell to display the title and content, respectively.
(3) and in the Attributes panel, set the tag value of two labels to 1 and 2 respectively for the code to get the label.
(4) Finally, to allow two label labels to grow automatically, set their Lines property to 0.
3, constraint settings
In addition to the cell cell need to use the Auto Layout constraint, the label within the cell is also set to the correct constraint, otherwise the Self sizing Cells cannot be implemented.
(1) The first Label (used to display the caption) sets up or down 4 constraints.
(2) The second Label, which is used to display a description of the content, sets the next 3 constraints.

Example

Import Uikit

Class Viewcontroller:uiviewcontroller, Uitableviewdelegate, Uitableviewdatasource {

var catalog = [[String]] ()

@IBOutlet weak var tableview:uitableview!

Override Func Viewdidload () {
Super.viewdidload ()

Initializing a list of data
Catalog.append (["section I: Swift environment set-up",
"As the swift development environment needs to run on the OS X system, let's take a look at how the swift development environment is built." "])
Catalog.append (["section II: Swift basic Syntax.") The title is long and long. ",
"This section describes some of the commonly used keywords in swift. As well as the introduction, annotation and other related operations. "])
Catalog.append (["section III: Swift data Type",
"Swift provides a wealth of data types, such as Int, UInt, floating-point numbers, Boolean values, strings, characters, and so on." "])
Catalog.append ([section Fourth: Swift variable ",
"Swift each variable specifies a specific type that determines the size of the variable's footprint." "])
Catalog.append ([Fifth Quarter: Swift Optional (optionals) type ",
"Swift's optional (Optional) type for handling value deletions. "])

Create a table view
Self.tableView.delegate = Self
Self.tableView.dataSource = Self

Set the default value for the Estimatedrowheight property
Self.tableView.estimatedRowHeight = 44.0;
The RowHeight property is set to Uitableviewautomaticdimension
Self.tableView.rowHeight = uitableviewautomaticdimension;
}

In this case, there is only one partition
Func Numberofsectionsintableview (Tableview:uitableview)-> Int {
return 1;
}

Returns the number of table rows (that is, returns the number of controls)
Func TableView (Tableview:uitableview, numberofrowsinsection section:int)-> Int {
Return Self.catalog.count
}

Create each cell display (create a parameter indexpath a specified cell)
Func TableView (Tableview:uitableview, Cellforrowatindexpath Indexpath:nsindexpath)
-> UITableViewCell
{
Let cell = Tableview.dequeuereusablecellwithidentifier ("MyCell",
Forindexpath:indexpath) as UITableViewCell
Get the corresponding entry contents
Let entry = Catalog[indexpath.row]
Let Titlelabel = Cell.viewwithtag (1) as! Uilabel
Let Subtitlelabel = Cell.viewwithtag (2) as! Uilabel
Titlelabel.text = entry[0]
Subtitlelabel.text = entry[1]
return cell
}

Override Func didreceivememorywarning () {
Super.didreceivememorywarning ()
}
}

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.