Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initializing a UITableView with "inserted" Rows

I've got a UITableView that I want to insert and delete rows in.

If I have an array of 10 values and start with the tableview including all rows, the table displays fine. Then if I want to filter out everything except [0, 1, 5]. I then go through the process of deleting rows at index paths: [2, 3, 4, 6, 7, 8, 9] and the returned number of rows is 3. I don't have to change my cell rendering code, it displays rows: [0, 1, 5].

However, if after having filtered everything, I want to come back to this table and load from persistence the table with rows [0, 1, 5], I'm having trouble. The table loads with number of rows: 3 and renders cells: [0, 1, 2].

I've tried initializing the table in viewDidLoad with 0 rows and then in viewDidAppear calling insertRows:[0, 1, 5] - but this throws an exception:

*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-2380.17/UITableView.m:909

And I've verified that in viewDidLoad, the number of rows is printing 0, and just before this error is thrown, the number of rows printed is 3.

EDIT If I insert instead [0, 1, 2] - it works... but again... I ultimately need [0, 1, 5] showing up.

 - (void) viewDidAppear:(BOOL) animated
 {
    [super viewDidAppear:animated];

    NSArray *initializing_indeces = @[[NSIndexPath indexPathForRow:0 inSection:0], [NSIndexPath indexPathForRow:1 inSection:0], [NSIndexPath indexPathForRow:5 inSection:0]];
    [self.userDataTable insertRowsAtIndexPaths:initializing_indeces withRowAnimation:UITableViewRowAnimationAutomatic];
}
- (int) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Using different booleans, but the gist:
    if (viewDidLoad)
        return 0;
    if (viewDidAppear || filtered)
        return 3;
    if (expanded)
        return 10;
}
- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"];

    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CELL"];
    }


    [cell.textLabel setText:[NSString stringWithFormat:@"%i",indexPath.row]];

    return cell;
}
like image 200
FishStix Avatar asked Aug 12 '26 13:08

FishStix


1 Answers

I have to keep track of the mapping myself:

- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"];

    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CELL"];
    }


    int row = indexPath.row;
    if (filtered) {
        // Translate 0 - 0
        // Translate 1 - 1
        // Translate 2 - 5
    }

    [cell.textLabel setText:[NSString stringWithFormat:@"%i", row]];

    return cell;
}

I ended up using an NSMutableArray that, when filtered, becomes [0, 1, 5] so accessing the index of that array, indexPath.row of 2 returns 5.

like image 196
FishStix Avatar answered Aug 15 '26 05:08

FishStix



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!