banner



How To Add Text To A Table In Xcode

UITableViewController

UIKit, Xcode thirteen & iOS 15

Developers without prior iOS knowledge

This tutorial demonstrates mutual use cases for the UITableView and UITableViewController classes.

Configure table contents using the Storyboard:
Configuring table contents using the Storyboard

Provide section and row content programmatically:
Provide section and row content programmatically

Bear witness data from a Swift Array:
Table View with data from Swift Array

Configure cells using cell styles:
Fruits Images Cell

Using Custom cells:
Using Custom cells to customize UITableViewCells

Grouping cells into sections:
Grouping cells

Making table cells reorderable:
Reorderable/Movable table cells

Requirements

For this tutorial you need basic programming skills and know how to utilise Xcode.

Creating an example project

  1. Run Xcode and create a new project with File » New » Project ⌘⇧N. Select iOS » Application » App:

    File » New » Project » iOS » Application » Single View Application

  2. Proper noun the app "Countries".
    Cull Swift equally Language, select Storyboard for User Interface.

    Projekteinstellungen Xcode-Projekt

  3. Use Edit » Delete ⌘⌫ to remove the existing controller from the storyboard.

    Bestehenden ViewController entfernen

  4. Also remove the existing source lawmaking file ViewController.swift (confirm the deletion with 'Movement to Trash' to besides delete the file from the project folder).

Configure table contents using the Storyboard

  1. Open Main.storyboard and elevate in a new Table View Controller from the Object Library:

  2. Select the controller and configure the controller as Initial View Controller:

  3. Shift-Click on the table to see all objects at that position and select Table View:

  4. In the Attributes Inspector configure Static Cells for Content to configure the tabular array cells in the storyboard instead of writing code to provide the data. Set the Mode of the Table View to Grouped:

    Table View: Static Cells + Style Grouped

  5. Show the Certificate Outline and familiarize yourself with the object structure. The Table View Controller manages its Table View consisting of Table View Cells and an empty Content View in the jail cell:

    Outline View for Table View Controller

  6. Select the section using the outline or with Shift + Click. Set Countries equally Header text:

  7. Select the cells past Shift + clicking and configure Bones as Style for all cells. A Basic Jail cell has one Label past default:

  8. Gear up some country names as text for the cells:

  9. Run the app with ⌘R.

Provide section and row content programmatically

  1. Utilise the projection from the previous steps or download a starter project here: Countries-static-cells.zip

  2. Use File » New » File ⌘Due north to create a new Cocoa Touch Course. Make sure that iOS is selected to create a source file for iOS:

    Cocoa-Touch-Klasse erstellen

  3. Create a subclass of UITableViewController in Swift and name information technology CountriesTableViewController:

    Klassenname für UITableViewController-Klasse

  4. Open the storyboard and ready CountriesTableViewController equally custom class for the Tabular array View Controller:

    Custom Class festlegen

  5. Select the Table View and configure Dynamic Prototypes for Content to define a image for the cells in the storyboard but to provide the content in the controller course. Configure ane prototype cell and select Plainly as Manner:

  6. Select the Prototype jail cell and configure CountryCell every bit Identifier. The identifier is used in the controller implementation to create cells co-ordinate to the prototype from the Storyboard:

  7. In the CountriesTableViewController class, customize the numberOfSectionsInTableView and tableView:numberOfRowsInSection: methods from the UITableViewDataSource protocol to return a fixed number of sections and rows and remove the #alarm comments:

                                    //                                Marker:                                  - Table view data source                override                func                numberOfSections                (                in                tableView                :                UITableView                )                ->                Int                {                                  return                  3                                }                override                func                tableView                (                _                tableView                :                UITableView                ,                numberOfRowsInSection                department                :                Int                )                ->                Int                {                                  return                  v                                }              
  8. Uncomment the tableView:cellForRowAtIndexPath: method in the controller class and customize it to create cells according to the Image cell and configure the prison cell text to evidence the section and row numbers:

                                    override                func                tableView                (                _                tableView                :                UITableView                ,                cellForRowAt                indexPath                :                IndexPath                )                ->                UITableViewCell                {                permit                cell                =                tableView                .                dequeueReusableCell                (                withIdentifier                :                                  "CountryCell"                                ,                for                :                indexPath                )                                  cell                  .                  textLabel                  ?.                  text                  =                  "Section                                    \(                  indexPath                  .                  section                  )                                      Row                                    \(                  indexPath                  .                  row                  )                  "                                return                prison cell                }              
  9. Overwrite the tableView:titleForHeaderInSection: method using the Xcode code completion:

    Overwrite tableView:titleForHeaderInSection: method

    Implement the method to render a title according to the section number:

                                    override                func                tableView                (                _                tableView                :                UITableView                ,                titleForHeaderInSection                department                :                Int                )                ->                String                ?                {                                  return                  "Section                                    \(                  department                  )                  "                                }              
  10. Remove the remaining lawmaking in the class, this should be the lawmaking in the class:

                                    import                UIKit                form                CountriesTableViewController                :                UITableViewController                {                //                                MARK:                                  - Table view data source                override                func                numberOfSections                (                in                tableView                :                UITableView                )                ->                Int                {                return                three                }                override                func                tableView                (                _                tableView                :                UITableView                ,                numberOfRowsInSection                section                :                Int                )                ->                Int                {                render                5                }                override                func                tableView                (                _                tableView                :                UITableView                ,                cellForRowAt                indexPath                :                IndexPath                )                ->                UITableViewCell                {                permit                cell                =                tableView                .                dequeueReusableCell                (                withIdentifier                :                "CountryCell"                ,                for                :                indexPath                )                cell                .                textLabel                ?.                text                =                "Section                                \(                indexPath                .                section                )                                  Row                                \(                indexPath                .                row                )                "                return                cell                }                override                func                tableView                (                _                tableView                :                UITableView                ,                titleForHeaderInSection                section                :                Int                )                ->                String                ?                {                render                "Department                                \(                section                )                "                }                }              

    Hint: The "// MARK: " comment groups the methods, for example when choosing a method from the Bound Bar:

  11. Run the app with ⌘R:

    Table showing the section and row

Show information from a Swift Array

  1. Use the project from the previous steps or download a starter project here: Countries-dynamic-prototypes.zip

  2. Add a type State to concur some example data and add together an Array with a few instance countries to the CountriesTableViewController:

                                                      struct                  Country                  {                  var                  isoCode                  :                  String                  var                  name                  :                  Cord                  }                                class                CountriesTableViewController                :                UITableViewController                {                                  allow                  countries                  =                  [                  Country                  (                  isoCode                  :                  "at"                  ,                  name                  :                  "Austria"                  ),                  Country                  (                  isoCode                  :                  "be"                  ,                  name                  :                  "Belgium"                  ),                  Country                  (                  isoCode                  :                  "de"                  ,                  name                  :                  "Germany"                  ),                  Country                  (                  isoCode                  :                  "el"                  ,                  name                  :                  "Greece"                  ),                  Country                  (                  isoCode                  :                  "fr"                  ,                  name                  :                  "France"                  ),                  ]                                //                                Marking:                                  - Table view information source                }              
  3. Customize the CountriesTableViewController to use data from the Assortment:

                                    class                CountriesTableViewController                :                UITableViewController                {                // ...                //                                MARK:                                  - Table view information source                override                func                numberOfSections                (                in                tableView                :                UITableView                )                ->                Int                {                render                                  1                                }                override                func                tableView                (                _                tableView                :                UITableView                ,                numberOfRowsInSection                department                :                Int                )                ->                Int                {                return                                  countries                  .                  count                                }                override                func                tableView                (                _                tableView                :                UITableView                ,                cellForRowAt                indexPath                :                IndexPath                )                ->                UITableViewCell                {                let                cell                =                tableView                .                dequeueReusableCell                (                withIdentifier                :                "CountryCell"                ,                for                :                indexPath                )                                  permit                  land                  =                  countries                  [                  indexPath                  .                  row                  ]                  jail cell                  .                  textLabel                  ?.                  text                  =                  state                  .                  name                                return                prison cell                }                }              

    Hint: You can besides remove the numberOfSections method - by default tables have one section.

  4. Run the app with ⌘R:

Configure cells using cell styles

  1. Apply the project from the previous steps or download a starter projection here:
    Countries-tableviewcontroller-swift-array.zip

  2. There are four different types of prison cell styles that take views out of the box - Basic, Left/Right Detail and Subtitle:

    UITableViewController Cell Styles

  3. For the CountriesTableViewController in the storyboard, select the prototype jail cell and set the style Subtitle:

    Cell Styles

  4. Ready a row pinnacle of 60 for the cell:

    Cell Styles

  5. Download country-images.aught and drag the images to the Assets.xcassets in your project:

  6. Extend the implementation of tableView(cellForRowAt:) in CountriesTableViewController to show a particular text and an image (all cells take an optional imageView that is created when the holding is accessed):

                                    class                CountriesTableViewController                :                UITableViewController                {                ...                override                func                tableView                (                _                tableView                :                UITableView                ,                cellForRowAt                indexPath                :                IndexPath                )                ->                UITableViewCell                {                allow                cell                =                tableView                .                dequeueReusableCell                (                withIdentifier                :                "CountryCell"                ,                for                :                indexPath                )                permit                country                =                countries                [                indexPath                .                row                ]                cell                .                textLabel                ?.                text                =                state                .                proper noun                                  cell                  .                  detailTextLabel                  ?.                  text                  =                  country                  .                  isoCode                  jail cell                  .                  imageView                  ?.                  epitome                  =                  UIImage                  (                  named                  :                  country                  .                  isoCode                  )                                return                prison cell                }                }              
  7. Run the app and check that the tabular array looks correct:

More UITableViewController tutorials

Using Custom cells:
Using Custom cells to customize UITableViewCells

Grouping cells into sections:
Grouping cells

Making table cells reorderable:
Reorderable/Movable table cells

More than information

How To Add Text To A Table In Xcode,

Source: https://www.ralfebert.com/ios-examples/uikit/uitableviewcontroller/

Posted by: georgewithen.blogspot.com

0 Response to "How To Add Text To A Table In Xcode"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel