Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I populate two sections in a tableview with two different arrays using swift?

I have two arrays Data1 and Data2 and I want to populate the data within each of these (they contain strings) into a tableview in two different sections.

The first section should have a heading "Some Data 1" and the second section should be titled "KickAss".

I have both sections populating with data from the first array (but with no headings either).

Here is my code so far:

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {     return 2 }  override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {     var rowCount = 0     if section == 0 {         rowCount = Data1.count     }     if section == 1 {         rowCount = Data2.count     }     return rowCount }  override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {     let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell     let ip = indexPath     cell.textLabel?.text = Data1[ip.row] as String     return cell } 

in the cellForRowAtIndexPath method, is it possible for me to identify the section somehow like I did in the numberOfRowsInSection method?

Also, how do I give titles to each section?

like image 834
Woody Avatar asked Apr 11 '15 14:04

Woody


People also ask

How do you give space between Tableview cells in Swift?

The basic idea is to create a new section (rather than a new row) for each array item. The sections can then be spaced using the section header height. Set up your project as described in UITableView example for Swift. (That is, add a UITableView and hook up the tableView outlet to the View Controller).

How to show activity in a UITableView in Swift?

Show activity on this post. You can determine which section you are in by looking at indexPath.section . To specify the titles, override the function func tableView (tableView: UITableView!, titleForHeaderInSection section: Int) -> String! Show activity on this post. In Swift 4 or Swift 5 you can use the code below.

How do I get the number of sections in a Tableview?

All table views require two data source methods. They return to the Table View how many sections and how many rows in each section. Add the following to your code: override func numberOfSectionsInTableView (tableView: UITableView) -> Int {

How to add two models to a UITableView?

Add the two models as properties under the class definition: All table views require two data source methods. They return to the Table View how many sections and how many rows in each section. Add the following to your code: override func numberOfSectionsInTableView (tableView: UITableView) -> Int { // Return the number of sections.

How do I populate the first section of an array?

The first section should have a heading "Some Data 1" and the second section should be titled "KickAss". I have both sections populating with data from the first array (but with no headings either).


1 Answers

TableView Cells

You could use a multidimensional array. For example:

let data = [["0,0", "0,1", "0,2"], ["1,0", "1,1", "1,2"]] 

For the number of sections use:

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {     return data.count } 

Then, to specify the number of rows in each section use:

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {     return data[section].count } 

Finally, you need to setup your cells:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {     let cellText = data[indexPath.section][indexPath.row]      //  Now do whatever you were going to do with the title. } 

TableView Headers

You could again use an array, but with just one dimension this time:

let headerTitles = ["Some Data 1", "KickAss"] 

Now to set the titles for the sections:

override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {     if section < headerTitles.count {         return headerTitles[section]     }      return nil } 

The code above checks to see there's a title for that section and returns it, otherwise nil is returned. There won't be a title if the number of titles in headerTitles is smaller than the number of arrays in data.

The Result

enter image description here

like image 186
ABakerSmith Avatar answered Sep 25 '22 22:09

ABakerSmith