Lucas
Lucas

Reputation: 65

Create row header using table view Swift

I have an array of an array of strings At top level:

 Array = [[November 1 2016], [November 2 2016], [November 3 2016, ..., [Current    Date]]

In array [November 1 2016], it contains a strings [timeOne, timeTwo, timeThree...]. Similar strings occur under the array [November 2 2016] and on.

The goal is to create a table that has a row showing the date as shown in the top level array, for example "November 1 2016". And then under that top row have all the values (timeOne, timeTwo etc.) from that arrow in individual cells. For example

 Row 1 = November 1 2016
 Row 2 = timeOne
 Row 3 = timeTwo
 Row 4 = timeThree
 Row 5 = November 2 2016

I have a function that creates an Array of the "timeOne, timeTwo, timeThree..." and then I can add that to the appropriate array. The problem with my method of creating an array inside an array is that multiple timeValues will be taken each day. How can I get swift to only record the date once each day the app is used and create a new array for this day. (for example if app not opened November 3, then the array includes November 2, November 4. But skips November 3.

Essentially what I am after is a table that looks kind of like this, but with only one column. Where it says 7:00am, 8:00am etc. will be my timeOne and timeTwo respectively

Is there a better way of going about this? End goal from UI tableview

Upvotes: 0

Views: 1784

Answers (1)

Jacob King
Jacob King

Reputation: 6157

The best way is to utilise the sections functionality of UITableView. In your case, each date would be it's own section, with the times subsequently being rows.

For example you could do something like the following:

tableView(tableView: UITableView, viewForHeaderInSection: Int) -> UIView? {
    let label = UILabel(frame: CGRectMake(0, 0, tableView.frame.width, 20))
    label.text = dates[section] // Would set to the date in the array.
}

Then in cellForRowAtIndexPath iterate through the times and display those.

If you need me to elaborate on anything just say.

EDIT:

A very crude implementation would look a little like this, obviously you'll need to populate the date and time array values.

class Time {
    var timeString: String!
    var eventString: String!
}

class Date {
    var dateString: String!
    var times: [Time]!
}

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    var dates = [Date]()

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = UITableViewCell()
        cell.textLabel?.text = dates[indexPath.section].times[indexPath.row].timeString
        return cell
    }

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

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

    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let label = UILabel(frame: CGRectMake(0, 0, tableView.frame.width, 20))
        label.text = dates[section].dateString
        return label
    }
}

Upvotes: 1

Related Questions