Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Segue in UITableView with multiple sections, each containing objects filtered from a single array

I'm a beginner, clearly out of my league and I haven't been able to find an answer online.

I have a UITableViewController with a UITableViewshowing custom objects stored in one array. I don't show all the object of the array in one single section of said TableView: the TableView has multiple sections, each containing a filtered portion of my objects array (I filter the custom objects array checking that the object category property is equal to a category that I specified in a categories array).

This filtering and showing the single array in different sections is working fine (I understand that maybe it's not elegant, as I said I'm a beginner in coding and I absolutely needed to work with one single array, without creating other arrays corresponding to the filtered results), but to better understand my issue I think it's better that I show what I did, so here's the TableView part of my code:

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

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        for (var i = 0; i <= section; i++){
            if section == i {
                for eachCategory in myCategoriesArray {
                    return myObjectsArray!.filter() { $0.objectCategoryProperty == myCategoriesArray[i] }.count
                }
            }
        }
    // ...
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {   
        var cell = tableView.dequeueReusableCellWithIdentifier("objectCell", forIndexPath: indexPath) as UITableViewCell
        for (var i = 0; i <= indexPath.section; i++){
            if indexPath.section == i {
                for eachCategory in myCategoriesArray {
                    cell.textLabel?.text = myObjectsArray!.filter() { $0.objectCategoryProperty == myCategoriesArray[i] }[indexPath.row].nameProperty
                    return cell
                }
            }
        }
    // ...
}

This works in the sense that I have the UITableViewController showing all my objects, but filtered in separated sections by category.

My issue is with the segue when I select a cell and show a detail view.

Here's my prepareForSegue method:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        var nextVC = segue.destinationViewController as MyNextViewController
        if let indexPath = tableView.indexPathForSelectedRow() {
            let selected = myObjectsArray![indexPath.row]
            nextVC.passedObject = selected
        }
    }
}

I'm sure that many of you already see my issue: the object that I pass to the next ViewController is selected in the custom objects array using as index [indexPath.row], but indexPath.row starts at 0 for each section, so when I select an object its index in the TableView is not equal to the index in the custom objects array, meaning that I pass the wrong object.

Now, I'm stuck because I don't see a way to pass the right (meaning, selected) object to the next View Controller while preserving the fact that I'm working with only a single array.

I was toying with the idea of adding an objectIDString property to every object and a single var currentlySelectedObjectIDString that is set every time a cell is selected and try to pass to the next View Controller the object with the objectIDString property matching the currentlySelectedObjectIDString, but it looks like a bad idea to my inexperienced eyes and I'm actually not sure how I could accomplish that even if I wanted to (maybe implementing didSelectRowAtIndexPath:, but I have not been able to make it work).

Any help would be really appreciated, I've been stuck on this for so long I begin to question a) my sanity b)every decision I made so far in the project (meaning, the single array for all objects that is filtered in sections), but I'm already so invested in it that I really would like not to have to start over.

Thank you,

Cesare

P.S. I hope my question is clear, english isn't my main language... sorry for any mistake!

like image 449
cdf1982 Avatar asked Jun 23 '26 13:06

cdf1982


1 Answers

I suggest you, to use a NSFetchedResultsController. This class have a property sectionNameKeyPath. In this property you could set your category and you won't need more iterate with a repetition in each numberOfSection and numberOfRows.

like this:

    let aFetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: moc, sectionNameKeyPath: "event.startDate", cacheName: nil)

In my case i need filter data by event.startDate.

I don't know if you are using core data, but if you are using, this is the better way to do this.

I'll expose them for you! In the first moment NSFetchedResultsController like complicated, but its very very useful. Don't be afraid.

I don't know exactly your model and data. In this case i'll show you my owner sample.

Please see my question in the following link: Sectioning TableView and rows with Core Data Swift

In this link, see my question, and in the bottom i'll explain the complete solution with the others answer.

If this is not clear for you, please, talk with me :.)

like image 62
Weles Avatar answered Jun 26 '26 09:06

Weles