Ios - Expand table view table like Twitter app

I want to have the same behavior as the Twitter app when I select a tweet that: expands the string and adds additional content.

So it's not just about resizing the main line. I think I need 2 different custom cells, so I did something like this:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([indexPath compare:self.selectedIndexPath] != NSOrderedSame) {
        FirstCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FirstCell"];
        if (!cell) {
            cell = [[FirstCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"FirstCell"];
        }

        cell.firstnameLabel.text = [[self.items objectAtIndex:indexPath.row] objectForKey:@"firstname"];

        return cell;
    }
    else {
        SecondCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SecondCell"];
        if (!cell) {
            cell = [[SecondCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SecondCell"];
        }

        cell.firstnameLabel.text = [[self.items objectAtIndex:indexPath.row] objectForKey:@"firstname"];
        cell.lastnameLabel.text = [[self.items objectAtIndex:indexPath.row] objectForKey:@"lastname"];

        return cell;
    }

}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([indexPath compare:self.selectedIndexPath] == NSOrderedSame) {
        return 80.0;
    } else {
        return 44.0;
    }
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.selectedIndexPath = indexPath;

    [tableView reloadData];
}

      

My problem is that I want to keep a fluid animation like a Twitter app, which is not my case due to [tableView reloadData]

(which is a must, I think, since I have two different custom cells).

So, does anyone know a workaround for my needs or maybe someone knows how the Twitter app handles this animation stuff?

+3


source to share


3 answers


You want to reload this cell with animation:



[tableView beginUpdates];
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];

      

+7


source


[tableView beginUpdates];
[tableView endUpdates];

      

this causes the cell row sizes to update for the whole tableView .... link from: iPhone - smooth animation to change the height of the UITableViewCell, including content update



Happy coding!

+3


source


It's actually quite simple to implement this using the two custom cells that you originally planned, just by combining the source code with rooster117's solution. Replace [tableView reloadData]

in tableView:didSelectRowAtIndexPath:

with:

[tableView beginUpdates];
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[tableView endUpdates];

      

and you should have your solution.

+1


source







All Articles