IPhone reloadRowsAtIndexPaths weird animation
Hi I'm new to this iPhone dev and I have a question about reloadRowsAtIndexPaths. When the user taps one cell, it will expand showing buttons underneath it and tapping it again closes the cell. This works f开发者_开发技巧ine when opening and closing cell one at a time, but I have this weird animation when tapping cells in sequence like cell one is tapped, cell 2 is tapped the animation gets weird. Please see the video to fully understand the situation and sorry for the low quality.
http://www.youtube.com/watch?v=R28Rmti9wPQ
and here's the code for reloading the cells
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:
(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
selectedIndexPath = indexPath;
//close the selected cell if it's open
if(selectedIndex == indexPath.row)
{
selectedIndexPath = nil;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
selectedIndex = -1;
selectedIndexPath = indexPath;
return;
}
//this will reload the previously open cell to avoid it from being recycled
if(selectedIndex >= 0)
{
NSIndexPath *previousPath = [NSIndexPath indexPathForRow:selectedIndex inSection:0];
selectedIndex = indexPath.row;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:previousPath] withRowAnimation:UITableViewRowAnimationFade];
}
//now reload the selected cell
if(selectedIndexPath != nil && [selectedIndexPath isEqual:indexPath]) {
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
and here's the code for creating the cellForRowAtIndexPath.
if(selectedIndexPath != nil && [selectedIndexPath isEqual:indexPath]) {
selectedIndex = indexPath.row;
static NSString *CustomCellIdentifier = @"CustomCell";
CustomCell *customCell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier: CustomCellIdentifier];
if (customCell == nil) {
customCell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CustomCellIdentifier] autorelease];
}
customCell.locationLabelOutlet.text = [usgsFeeds objectAtIndex:[indexPath row]];
customCell.dateLabelOutlet.text = [pubDateArr objectAtIndex:[indexPath row]];
float thisMag;
thisMag = [(NSNumber *)[magnitudeArr objectAtIndex:[indexPath row]] floatValue];
customCell.magnitudeImageOutlet.image = [self imageForMagnitude:thisMag];
[customCell setSelectionStyle:UITableViewCellSelectionStyleNone];
return customCell;
}
else{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
locationLabel = [[[UILabel alloc] initWithFrame:CGRectMake(10, 3, 225, 20)] autorelease];
locationLabel.tag = kLocationLabelTag;
locationLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:14.0];
locationLabel.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:locationLabel];
//and all other views for the cell....
Try reloading both the cell you just selected and the one that was selected before.
This is how I solved a similar problem in my own app.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if( selectedPath == indexPath)
{
return;
}
showNutritionInfo = NO;
NSIndexPath *oldSelectedPath = selectedPath;
selectedPath = indexPath;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, oldSelectedPath,nil] withRowAnimation:UITableViewRowAnimationFade]; }
精彩评论