"See All" cell for UITableView sections.

Discussion in 'iOS Programming' started by mdhansen5, Jun 1, 2012.

  1. mdhansen5 macrumors member

    Joined:
    Nov 22, 2010
    Location:
    Colorado
    #1
    I've got a UITableView thats using NSFetchedResultsController and Core Data.

    A user is able to create a new table entry that is divided into 3 UITableView sections, 0, 1 & 2.

    Instead of having a huge scrolling list full of items, I want to be able to maybe show the top 1 or 2 cells in each section and then have 1 more cell that says "see all" or "X more entries" or something like that, which allows them to see all of the entries that are supposed to be in the section, either by expanding the section, or taking them into another TableView which just shows the cells in that section (that should be easier).

    Is something like this possible? I can provide some code if it helps!

    I'm not using a grouped style tableview, just to clear any confusion.

    Thanks to anyone that can help me out!
     
  2. ppilone macrumors 6502

    Joined:
    Jan 20, 2008
    #2
    Someone might come along and prove me wrong but since you're using an NSFetchedResultsController to managed the table view it'll be difficult to customize it the way you describe. The NSFetchedResultsController is a finicky beast, very good at doing what it does but is not all that customizable.

    Since you're already talking about cutting the sections down to a specific number of rows do you really need the fetched results controller? One of the benefits of the NSFetchedResultsController is managing the memory of a large list of items. Since you want to filter the sections anyway you can handle fetching a specific number of items for each section and manage them yourself. When the user taps the "See all ->" row it can then push a view controller that then uses an NSFetchedResultsController to show/manage all of the data from that section.
     
  3. mdhansen5 thread starter macrumors member

    Joined:
    Nov 22, 2010
    Location:
    Colorado
    #3
    Alright, I'll keep that in mind. Thanks for the reply.
     
  4. MattInOz macrumors 68030

    MattInOz

    Joined:
    Jan 19, 2006
    Location:
    Sydney
    #4
    It should be a fun challenge for you. As ppilone suggested NSFetchedResultsController can be finicky. Let it do what it does then use your own logic to decide when you when you ask it for information to feed the TableView

    The NSFetchedResultController doesn't manage the UITableView directly.
    Your viewController sits in the middle, so you put some code in the way that might be able to do what you want. eg. The -tableview:numberOfRowsInSection you can return what ever number you like, although it breaks if you return too many rows. So if you had a way to see if a section had been expanded then you could return the value yourFetchedResultsController gives you, if not return a limited number.

    You'll need to do similar things in many of the other methods in your viewController that feed data from the yourFetchedResultsController to yourTableView to get the behaviour you want.
     
  5. CodeBreaker macrumors 6502

    Joined:
    Nov 5, 2010
    Location:
    Sea of Tranquility
    #5
    The good thing is that the FetchedResultsController follows an index path (section/row) pattern similar to the table view. You can keep a BOOL, something like shouldShowAllCells as an ivar/property of your Table View Controller.

    For your numberOfRows method, you can do something like this:

    Code:
    - (NSInteger)numberOfRowsInSection:(NSInteger)section {
        if (!_shouldShowAllRows) {
            return 3; // 2 actual rows + 1 row that says "Show more"
        }
        
        //Return actual rows by quering the FetchedResultsController here
    }
    
    You will have to create a "Show more" cell in cellForRowAtIndexPath:

    Code:
    - (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        //Dequeue a cell and if it is nil, alloc one here
        
        if(!_shouldShowAllRows && indexPath.row == 2) {
            [cell.textLabel setText:@"Show more"];
        } else {
            //show actual cell data
        }
    }
    
    
    And finally, in didSelectRowAtIndexPath: you will have to check if the "Show more" cell is selected:

    Code:
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        if (!_shouldShowAllRows && indexPath.row == 2) {
            _shouldShowAllRows = YES;
            
            [tableView reloadData]; // or reload the section
            
        } else {
            //Normal cell selection flow here
        }
    }
    
    PS: This is browser code.
     
  6. ppilone macrumors 6502

    Joined:
    Jan 20, 2008
    #6
    CodeBreaker posts a really interesting solution. Just make sure in your NSFetchedResultsControllerDelegate callbacks you take into account your custom logic for updating the table. IIRC (it's been awhile) the boiler plate delegate code *always* tries to update the table whenever a model is added/deleted/updated etc.
     

Share This Page