Cees Cees - 4 months ago 8
iOS Question

Sections in UITabelView with API data

I am working in swift 2.x and xcode 7.x and i am stuck with tabelviews and api data. Everywere i look i need to have 2 array's one with my object and one for the section. my current code is:

//MARK: Setting up the tableview
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return eventsCollection.count
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
let eventDate = self.eventsCollection[section].startDate
let month = Utility.sharedInstance.convertToDateStringMonth(eventDate)
return month
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.eventsCollection.count
}


func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

return 70;
}


func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
return
}


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: EventCellView!
cell = self.calenderTabelView.dequeueReusableCellWithIdentifier("EventCellView") as! EventCellView
cell.dayNameLabel.text = Utility.sharedInstance.getDayOfWeek(self.eventsCollection[indexPath.row].startDate)
cell.dayNumberLabel.text = Utility.sharedInstance.convertToDataDay(self.eventsCollection[indexPath.row].startDate)
cell.NewLabel.text = self.eventsCollection[indexPath.row].Name
return cell
}


When i get the data i unwrap it and set it in the eventcollection. an exaple from an event is:

NOTE: this is the API response.

AllDay = 1;

Attendees = (
); <as array>
Description = "Test Evenment"; <as string>
EndDate = "2017-02-03T23:59:59+00:00"; <as string>
ID = 173; <as int or string>
Name = "Test evenement"; <string>
StartDate = "2016-11-01T00:00:00+00:00"; <string>
VenueName = "Appbakkers HQ";} <string>


when i run the code it devides the tabelview in sections with the name of the month as title header. But i need to sort the event into the right month. for example the one in june only needs to appear once in june and when i add an otherwone in june it needs to go there.

so tableview look like this:

june

cell for january

cell for june

january

cell for january

cell for june

what i want is

june:

cell for june

january

cell for january

the function getDayOfWeek -> monday thueseday ,etc as a string
the function convertToDataDay -> 1 - 31 as a string
the function convertToDateStringMonth -> june, juli, etc. as string


any input would help me along! Thanks for any replies.

ADDITIONAL:

class Event: NSObject {
//the event class
var id:Int = 0
var Description: String = ""
var endDate: String = ""
var Name: String = ""
var startDate: String = ""
var venueName: String = ""
// var Attendees: NSDictionary = [:] havent figuerd this part out also
}

func mapComment(responseObject: AnyObject) -> Event{

let EventObject = Event()

// let andenteesObject = responseObject["Attendees"]

if let ID = responseObject["id"] as? Int {
EventObject.id = ID
} else if let ID = responseObject["id"] as? String {
EventObject.id = Int(ID)!
}

if let despription = responseObject["Description"] as? String{
EventObject.Description = despription
}
if let startDate = responseObject["StartDate"] as? String{
EventObject.startDate = startDate
}
if let endDate = responseObject["EndDate"] as? String{
EventObject.endDate = endDate
}
if let name = responseObject["Name"] as? String{
EventObject.Name = name
}
// if let attendees = andenteesObject as? NSDictionary{
// EventObject.Attendees = attendees
// }
return EventObject
}
}


so i have 2 questions actually:


  1. How to i sort my data in and month array

  2. How do i get my data from that array in the tableview?


Answer

Your approach is good, little modification is needed. Basically you need to maintain an attendees by month and separate dates array, here is how it can be done,

var eventsByMonth : [String : [Event]] = [:]

// store as date, its easy to sort (but anyway you like) 
var months = [NSDate]()

// iterate over response and store according to month,
for event in events {

  //convert the string to date (you can find it easily)
  let startDate = convertStringtoDate(event.startDate)

  months.append(startDate)

  // now split events by months 
  let month = Utility.sharedInstance.convertToDateStringMonth(event.StartDate)

  var temp = eventsByMonth[month] ?? []
  temp.append(event)

  eventsByMonth[month] = temp
}

months = sortDate(months) // sort anyway you like

so now you can use the months array to get the counts to the tableView like below,

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

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    let month =  months[section]

    guard let monthEvents = eventsByMonth[month] else {
        return 0
    }

    return monthEvents.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let month =  months[section]
    let monthEvents = events[month]!

    let event = monthEvents[indexPath.row]

    // configure your cell
}

hope that helps!