UITableViewCell在iOS 6中显示不正确

时间:2012-11-15 09:39:00

标签: objective-c uitableview ios6 uiactivityindicatorview

我在内部UITableViewCell显示UIActivityIndicatorView时遇到了令人烦恼的问题。我的tableView委托加载了部分数据。最后一个单元格始终指示加载过程,而未加载新部分。加载从tableView: willDisplayCell: forRowAtIndexPath:开始。因此,在iOS 5(或iOS 5模拟器)中,它运行良好,但在iOS 6(或iOS 6模拟器)中,UIActivityIndicatorView仅首次显示。也许有些东西因iOS 6或iOS 6的bug而被弃用了?

这是我的dataSource:

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
    UITableViewCell* cell = nil;
    if (_callback != nil && !_noMoreBooks && indexPath.row == [_books count])
    {
        cell = [tableView dequeueReusableCellWithIdentifier:LTTableViewLoadMoreCellIdentifier];
        if (cell == nil)
        {
            cell = [[[LTLoadMoreCell alloc] initWithStyle:UITableViewCellStyleDefault
                                          reuseIdentifier:LTTableViewLoadMoreCellIdentifier] autorelease];
            [(LTLoadMoreCell*)cell setRowTag:-1];
        }
    }
    else
    {
        cell = [tableView dequeueReusableCellWithIdentifier:LTTableViewCellIdentifier];
        if (cell == nil)
        {
            cell = [[[LTBookCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:LTTableViewCellIdentifier] autorelease];
            cell.selectionStyle = UITableViewCellSelectionStyleGray;
            ((LTBookCell*)cell)._hidePrice = NO;
        }
    }
    return cell;
}

这是我的代表:

- (void)tableView:(UITableView*)tableView willDisplayCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath*)indexPath
{
    if ([LTTableViewLoadMoreCellIdentifier isEqualToString:[cell reuseIdentifier]]
        && [(LTLoadMoreCell*)cell rowTag] != indexPath.row)
    {
        [(LTLoadMoreCell*)cell setRowTag:indexPath.row];
        NSUInteger remain = LT_STORE_BOOK_LIST_LIMIT - rowsLoaded;
        NSUInteger by = remain < LT_STORE_BOOKS_LOAD_PORTION ? remain : LT_STORE_BOOKS_LOAD_PORTION;
        _currentError = _callback(_genres, rowsLoaded, by);
        if (_currentError == LTNoInternetError)
        {
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
        }
        else if (_currentError != LTErrorNone)
        {
            _noMoreBooks = YES;
        }
    }
    else if ([LTTableViewCellIdentifier isEqualToString:[cell reuseIdentifier]])
    {
        if ((indexPath.row == rowsLoaded-1) && _currentError == LTNoInternetError)
        {
            NSUInteger remain = LT_STORE_BOOK_LIST_LIMIT - rowsLoaded;
            NSUInteger by = remain < LT_STORE_BOOKS_LOAD_PORTION ? remain : LT_STORE_BOOKS_LOAD_PORTION;
            _currentError = _callback(_genres, rowsLoaded, by);
            if (_currentError == LTErrorNone)
                [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewScrollPositionBottom];
        }
        LTBook* rowBook = [_books extraObjectAtIndex:indexPath.row];
        if (rowBook != nil)
        {
            ((LTBookCell*)cell)._book = rowBook;
            ((LTBookCell*)cell).isOdd = (indexPath.row % 2);
        }
    }
}

这是LoadMoreCell.m:

#import "LTLoadMoreCell.h"

@implementation LTLoadMoreCell

@synthesize rowTag;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString*)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

    if (self)
    {
        _activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        [_activityIndicatorView startAnimating];
        [[self contentView] addSubview:_activityIndicatorView];
        [self setUserInteractionEnabled:NO];
    }

    return self;
}

- (void)dealloc
{
    [_activityIndicatorView release];
    [super dealloc];
}

#pragma mark -
#pragma mark *** UITableViewCell methods ***
#pragma mark -

- (void)layoutSubviews
{
    [super layoutSubviews];
    [_activityIndicatorView setAutoresizingMask:UIViewAutoresizingNone];
    CGSize cellSize = [[self contentView] frame].size;
    [_activityIndicatorView setCenter:CGPointMake(cellSize.width / 2, cellSize.height / 2)];
}

@end

2 个答案:

答案 0 :(得分:5)

显然动画在某些时刻停止,我不明白为什么只在iOS 6中发生这种情况。所以我从init方法中删除了[_activityIndicatorView startAnimating];并添加到layoutSubviews这个:

if(![_activityIndicatorView isAnimating])
       [_activityIndicatorView startAnimating];

我认为这是iOS 6的错误。

答案 1 :(得分:2)

你只在你的单元格的INIT方法中调用[activyView startAnimating] ..尝试在你想要它动画之前调用它:: willDisplayCell甚至是viewForCell