我在顯示UITableViewCell
和UIActivityIndicatorView
時遇到了一個煩人的問題。我的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
我在iOS 6發佈後不久就遇到了同樣的事情 - 我們在表格單元格中顯示了一些活動指標,並使用IB'動畫'複選框,然後根據需要顯示/隱藏視圖。在iOS6上,它們出現,但不動畫。這種技術在表格單元之外的其他區域繼續正常工作。看起來他們打斷了我。 – antsyawn
同樣在這裏,在iOS 5中可以正常工作,但在iOS 6中不能正常工作!謝謝瓦倫丁! – Groot