2011-08-04 66 views
0

嗨,我是這個iPhone開發新手,我有一個關於reloadRowsAtIndexPaths的問題。當用戶點擊一個單元格時,它將展開顯示其下方的按鈕並再次點擊以關閉單元格。這在一次打開和關閉單元格時正常工作,但是當按細胞單元的順序輕敲單元格時,我有這種奇怪的動畫,單元格2被輕敲,動畫變得很奇怪。請參閱視頻以充分了解情況並對低質量感到抱歉。IPhone reloadRowsAtIndexPaths奇怪的動畫

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.... 

回答

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]; } 
+0

已經解決了。我只是在實際重新加載當前選定的單元格之前添加了一個延遲。 – Diffy