syedfa syedfa - 1 year ago 65
iOS Question

Need to display certain string elements in an array at the top of a UITableView in Swift

I have a UITableView which displays a list of strings from an array. The array is in alphabetical order, and at the top of the UITableView is a UISearchController bar. Everything is working fine. However, I need to make a modification to the list that is presented in the UITableView where a certain subset within the collection is presented at the top of the UITableView (and this subset should ALSO be in alphabetical order). However, once the user enters a character inside the search filter at the top, the list of strings displayed shouldn't matter.

For example, I have a UITableView which displays a list of employees in alphabetical order. What I want to do is when the UITableView loads, instead of presenting ALL the employees in alphabetical order, I would like to first list the managers in alphabetical order, and then the remaining employees in alphabetical order (i.e. AFTER the managers).

The array being received in the ViewController which holds the UITableView from the previous ViewController, which sends this list already sorted in alphabetical order. In other words, the array to begin with, is received already sorted. I hope this makes sense.

Thanks in advance to all who reply.

Answer Source

I'm assuming you don't want to use sections? That you just want them to all in the same section?

If that's the case you would probably need to do some preprocessing to split the array into your subsets (in viewDidLoad or somewhere else at the beginning of your controller's life cycle):

self.managerArray = [AnyObject]() // you'll need a property to hold onto this new data source
self.employeeArray = [AnyObject]()

for employee: Employee in self.allEmployees {
    // assume allEmployees is the alphabetical array
    if employee.isManager {
        // your condition for determining the subset
    else {

Because the array is already alphabetized this should populate the subarrays in alphabetical order as well (append just adds to the next index).

Then to make sure the table doesn't load the values before it's been processed in this way you'd need to have

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if self.employeeArray && self.managerArray {
        return self.employeeArray.count + self.managerArray.count
    return 0

Then you can just just populate cells from self.managerArray before moving onto self.employeeArray.

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    if indexPath.row < self.managerArray.count {
        var manager: Employee = self.managerArray[indexPath.row]
        // populate your cell info here
    else {
            // else we've already filled in manager's time to start on employees
            // subtract the number of managers from the indexPath to start at beginning of employeeArray
        var employee: Employee = self.employeeArray[indexPath.row - self.managerArray.count]
        // populate cell here
    return cell

Then when you search, you can search on the original self.allEmployees array like normal.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download