IPhone reloadRowsAtIndexPaths奇怪的动画

时间:2011-08-04 02:09:49

标签: iphone ios xcode

您好我是这个iPhone开发人员的新手,我有一个关于reloadRowsAtIndexPaths的问题。当用户点击一个单元格时,它将展开显示其下方的按钮并再次点击它关闭单元格。当一次打开和关闭一个单元格时,这可以正常工作,但是当按顺序敲击单元格时,我有这个奇怪的动画,单元格1被轻敲,单元格2被轻敲动画变得奇怪。请参阅视频以充分了解情况,并对低质量感到抱歉。

http://www.youtube.com/watch?v=R28Rmti9wPQ

这是重新加载单元格的代码

- (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];
}

}

这是创建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....

1 个答案:

答案 0 :(得分:0)

尝试重新加载刚刚选择的单元格和之前选择的单元格。

这就是我在自己的应用中解决类似问题的方法。

- (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]; }