Sorting array of custom objects into sections dynamically UITableview

I have a problem that has been plaguing me for days.

I have an array called countries. This array contains a custom object that contains data such as Country, City, IP, status, etc.

I need to represent these objects as a table separated by the "country" property.

This means that if I have two custom objects both with the country "Australia" I need to add it to the section "Australia" and if I have 5 objects with the country "Spain" I need these 5 objects in the section Spain.

I need to find a way to make sure that even if the object is added to another country, it gets under the section with the name of the country of the country.

I've tried various things, but nothing came close to fixing this. The reason this is dynamic is because I don't know how many objects there will be in each section. This means that the number of objects in each section may change.

I used this code to sort an array alphabetically. I just need to put each object in a section based on the Country property. I don't know how many sections there will be or how many objects there will be in each section.

self.countries = self.countries.sorted { $0.country < $1.country }

      

Can anyone give me a hand?

edit: added my cellForRowAtIndexPath.

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    if recievedData == true {
        var cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CountryListCell
        var serverInfo = countries[indexPath.row] as ServerInfoObject
        cell.countryTitle.text = serverInfo.country
        cell.flagImage.image = UIImage(named: serverInfo.flag)
        cell.serverInfo = serverInfo

        return cell
    }

    return CountryListCell()

}

      

+3


source to share


3 answers


So, you need to sort the servers in the dictionary. EG [String:[Server]]

string that is the section name (country). So an example dict["Albania":[serv1,server2,...]]

You can simply do this by sorting the server array (I assume you can figure it out).

You also need to store the array of section headers so you can access it in cellForRowAtIndexPath

. for example["Albania","Brazil","Cuba","Germany","Russia"]

So, basically when in cellForRowAtIndexPath

you access the current country with

var country = yourSectionArray[indexPath.section]

      

Then, access the array of servers that are valid for that country through your dictionary, for example:



var serversOfCurrentSection = yourDictionary[country] as [Server]

      

Then, to get the current server for the index, just do

var server = serversOfCurrentSection[indexPath.row]

      

To get the number of objects in each section:

var country = yourSectionArray[indexPath.section]
yourDictionary[country].count

      

+1


source


Have you tried using NSPredicate to help you filter your array by country name? I think you can either split your servers into an array of array in run, or at the start of the download, depending on whether the list is updated.

In numberOfRowsInSection:

just count the rows of the auxiliary array in the index section.



Likewise, cellForRowAtIndexPath:

use your sorted arrays to get information from your custom class.

I've had to deal with something similar to sorting contacts. Hope this helps!

0


source


There are many ways to do this.

Tables require stable data to function. If you change your data model, you will need to update the view in the table.

I would probably store the records in two ways: in a flat array and then in an array by country, where each record in that array was an array of records for that country. Collection objects such as arrays store objects within them by reference, so there isn't much overhead when storing the same records in multiple arrays.

This makes creating your split tables very clean.

You can sort your flat array by country and then loop through the sorted array by building your arrays of records by country. If you need your records sorted by some other criteria internally, you can use NSPredicate to sort, not sort, as it can accept an array of sort keys.

If the user can enter a new record anywhere, you can collect data for that new record, then find the location where it belongs in the sorted array, insert it into that location, and then rebuild your 2-dimensional array of arrays from the sorted flat matrix. This will avoid re-sorting the entire array, which can be slow for large datasets.)

0


source







All Articles