E
E
Eugene Kubesh2014-05-24 23:59:15
iOS
Eugene Kubesh, 2014-05-24 23:59:15

iOS breakout tables?

Good day. The question is the following:
I receive a list of ads from the server (20 each), each ad has a distance in kilometers from the user, the list is sorted by increasing distance. I want to make a table with sections "Up to 3 km", "Up to 15 km" and more than 15 km.
How to make a mechanism for loading / loading data into a table with sections?
Here is such an entry (ignore the if (i==0) this is not a valid condition,

NSMutableArray* newPaths = [NSMutableArray array];
         for (int i = (int)[self.petsArray count] - (int)[pets count]; i < [self.petsArray count]; i++) {
             if (i == 0)
             {
                 [newPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]];
             }
             else
             {
                 [newPaths addObject:[NSIndexPath indexPathForRow:i inSection:1]];

             }
         }
         
         [self.tableView beginUpdates];
         [self.tableView insertRowsAtIndexPaths:newPaths withRowAnimation:UITableViewRowAnimationTop];
         [self.tableView endUpdates];

swears
Invalid update: invalid number of sections. The number of sections contained in the table view after the update (2) must be equal to the number of sections contained in the table view before the update (2), plus or minus the number of sections inserted or deleted (0 inserted, 1 deleted
How to update a table correctly?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
ManWithBear, 2014-05-25
@Basters

Get 3 arrays for declarations for 1 section, for the 2nd and for the 3rd.
Add the following methods

- (NSInteger)numberOfSections {
    int count = 0;
    if (firstSectionArray.count) count++;
    if (secondSectionArray.count) count++;
    if (thirdSectionArray.count) count++;
    return count; 
}
- (NSInteger)numberOfRowsInSection:(NSInteger)section {
    switch (section) {
        case 0: return firstSectionArray.count; break;
        case 1: return secondSectionArray.count; break;
        case 2: return thirdSectionArray.count; break;
    }
    return 0;

When loading data, add them to the end of the corresponding array.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question