2012-11-15 74 views
3

我在顯示UITableViewCellUIActivityIndicatorView時遇到了一個煩人的問題。我的tableView委託加載部分數據。最後一個單元總是指示加載過程,而新部分未加載。加載在tableView: willDisplayCell: forRowAtIndexPath:開始。因此,在iOS 5(或iOS 5模擬器)中它可以很好地工作,但在iOS 6(或iOS 6模擬器)中UIActivityIndicatorView僅在第一次顯示。也許有些東西因爲iOS 6而被棄用,或者是iOS 6的bug?TableViewCell在iOS 6中顯示不正確

這裏是我的數據源:

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

回答

5

顯然動畫某一時刻停止,我不明白爲什麼會這樣,只有在iOS的6於是,我從init方法去除[_activityIndicatorView startAnimating];並添加到layoutSubviews此:

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

我認爲這是iOS 6中的bug 。

+0

我在iOS 6發佈後不久就遇到了同樣的事情 - 我們在表格單元格中顯示了一些活動指標,並使用IB'動畫'複選框,然後根據需要顯示/隱藏視圖。在iOS6上,它們出現,但不動畫。這種技術在表格單元之外的其他區域繼續正常工作。看起來他們打斷了我。 – antsyawn

+0

同樣在這裏,在iOS 5中可以正常工作,但在iOS 6中不能正常工作!謝謝瓦倫丁! – Groot

2

你調用[activyView startAnimating]在您單元的只有INIT方法..嘗試調用它之前,你想它動畫:: willDisplayCell甚至viewForCell

+0

你給了我很好的提示,我發現解決方案!謝謝! –

+0

:)那是什麼? /你做了什麼 - PLZ分享你的解決方案:) –

+0

看我的答案!對不起,我的英語:-) –