![]() Loading items into the array is done in func viewDidLoad() override func viewDidLoad() This creates an empty array of String for our data. In ViewController.swift put the property: var objects = () Using a Swift Array for Data As stated above we will use a Swift array for the data. We present the code implementation details below. We put the above functions in ViewController.swift since it serves as the dataSource and delegate for the Table View. The UITableViewDelegate protocol method used for this is: func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) ![]() Now we turn to the Table View’s delegate (UITableViewDelegate protocol) for responding to a user tapping on a cell. The UITableViewDataSource protocol functions that correspond to the above are: 1) func numberOfSectionsInTableView(tableView: UITableView) -> Int 2) func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int 3) func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell One data cell will be allocated for each item in the array. Our data will be supplied supplied via a Swift array. The number of sections in the Table View (in our case this is optional since we have only one section). It needs the following information to be able to create and configure data cells: 1. The job of the dataSource (UITableViewDataSource protocol) is to create and configure (put data in) the cells of Table View. With View Controller still selected, double click on its navigation bar and type in “Apple Products”. From the Editor menu, choose Embed In > Navigation Controller. Go to the Document Outliner and select View Controller. Push).įinally, we embed the View Controller in a Navigation Controller. ![]() Make sure the Segue type is set to Show (e.g. In the Document Outliner, select Show segue to New View Controller. ![]() In the Document Outline, control drag from View Controller to New View Controller. Make a segue (segway) from the View Controller to the New View Controller. This highlights the constraints in the editor. To see the three added constraints, select the Size Inspector and mouse over the constraints. Select the Label in the Document Outline and click on the “Resolve Auto Layout Issues” icon at the bottom right (last one). The Label needs some constraints to accommodate all devices. Control drag from the label to NewViewController.swift to create: weak var productLabel: UILabel! Option click on NewViewController.swift to put it in the Assistant editor. Using the Size inspector, put the Label at x=44, y=225, width=525 and height=21. From the Object Library, drag a Label into the View. In the Document Outline, select New View Controller’s View. We want a Label in New View Controller’s View. Use the pull-down menu and select NewViewController. In the Document Outline, select New View Controller. Set the Custom Class of New View Controller. The file NewViewController.swift appears in the Project Navigator. Select a Cocoa Touch Class file and press Next. Press the + button at the bottom left and select File. In the Project Navigator, select TableViewCell.swift. Name it New View Controller.Ĭreate a swift file to correspond to the newly created New View Controller. Select the just created View Controller in the Document Outline and click on it again to edit its name. From the Object Library, drag in another View Controller. When a user selects a cell in our table, we want to go to another view. Control drag from the label to TableViewCell.swift to create: weak var titleLabel: UILabel! Option click on TableViewCell.swift to put it in the Assistant editor. Using the Size inspector, put the Label at x=33, y=11. From the Object Library, drag a Label into the prototype cell. Note that in the Document Outline, Table View Cell has been changed to cell. Use the Accessory pull-down menu and select Disclosure Indicator. In the Document Outline, select Table View Cell. Use the pull-down menu and select TableViewCell (.swift) to be the custom class for Table View Cell.įor the Table View Cell, put in a Reuse Identifier and a Disclosure Indicator. The file TableViewCell.swift appears in the Project Navigator. In the Project Navigator, select ViewController.swift. Go to the Attributes inspector and set Prototype Cells to 1.Ĭreate a swift file to correspond to the newly created Table View Cell. In the Document Outline, select Table View. Close the Assistant Editor.Ĭreate a prototype cell. weak var tableView: UITableView!Īppears in ViewController.swift. In the Name field type tableView and press connect.
0 Comments
Leave a Reply. |