swift2 - Swift - How to group table using object array -


i have following class:

class providerschedule : anyobject {      var provideruid : string = ""     var lastname : string = ""     var firstname : string = ""     var scheduleregion : string = ""     var scheduleamount : float = 0      init () {      }       } 

then create array of object in order set source uitableview.

var tabledata : array<providerschedule> = [] 

my table functions are:

func tableview(tableview: uitableview!, numberofrowsinsection section: int) -> int {         return self.tabledata.count     }      func numberofsectionsintableview(tableview: uitableview!) -> int {         return 1     } 

what need group data by:

lastname , firstname name appears on group section of table.

any clue how that?

convert data format better suited table groups. make class represent group key, this:

class groupkey : hashable {     let firstname : string     let lastname : string     init(fn:string, _ ln:string) {         firstname = fn         lastname = ln     }     ... // implement hashable , equatable } 

now make array of groupkey objects, , sort them desired:

var groupset = set<groupkey>() s in tabledata {     groupset.insert(groupkey(s.firstname, s.lastname)) } let sortedgroups = array(groupset) sortedgroups.sort {     $0.firstname < $1.firstname || ($0.firstname == $1.firstname && $0.lastname < $1.lastname) } 

prepare schedule groups:

var schedulegroup = [[providerschedule]]() g in sortedgroups {     schedulegroup.append(         tabledata.filter{$0.firstname == key.firstname && $0.lastname == key.lastname}     ) } 

finally, implement methods supplying section information:

func numberofsectionsintableview(_ tableview: uitableview) -> int {     return sortedgroups.count } func tableview(_ tableview: uitableview, numberofrowsinsection section: int) -> int {     return schedulegroup[section].count } func tableview(_ tableview: uitableview, titleforheaderinsection section: int) -> string? {     let key = sortedgroups[section]     return "\(key.lastname), \(key.firstname)" } 

Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -