【问题标题】:IPhone reloadRowsAtIndexPaths weird animationiphone reloadRows At IndexPath 奇怪的动画
【发布时间】:2011-08-04 02:09:48
【问题描述】:

您好,我是这个 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....

【问题讨论】:

    标签: iphone ios xcode


    【解决方案1】:

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

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

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

    【讨论】:

    • 已经解决了。我只是在实际重新加载当前选定的单元格之前添加了一个延迟。
    猜你喜欢
    • 2012-07-19
    • 1970-01-01
    • 2019-01-12
    • 2015-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-27
    相关资源
    最近更新 更多