我對UITableView單元渲染的正確行爲感到非常困惑。這裏的情況:所有的UITableCells一次渲染......爲什麼?
我有一個250個項目加載到表視圖,每個圖像的列表。爲了優化圖片下載,我遵循Apple的LazyTableImages示例代碼...幾乎完全遵循它。真正優秀的系統...作參考,這裏的蘋果示例代碼中的單元格渲染器:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// customize the appearance of table view cells
//
static NSString *CellIdentifier = @"LazyTableCell";
static NSString *PlaceholderCellIdentifier = @"PlaceholderCell";
// add a placeholder cell while waiting on table data
int nodeCount = [self.entries count];
if (nodeCount == 0 && indexPath.row == 0)
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:PlaceholderCellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:PlaceholderCellIdentifier] autorelease];
cell.detailTextLabel.textAlignment = UITextAlignmentCenter;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
cell.detailTextLabel.text = @"Loading…";
return cell;
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
// Leave cells empty if there's no data yet
if (nodeCount > 0)
{
// Set up the cell...
AppRecord *appRecord = [self.entries objectAtIndex:indexPath.row];
cell.textLabel.text = appRecord.appName;
cell.detailTextLabel.text = appRecord.artist;
// Only load cached images; defer new downloads until scrolling ends
if (!appRecord.appIcon)
{
if (self.tableView.dragging == NO && self.tableView.decelerating == NO)
{
[self startIconDownload:appRecord forIndexPath:indexPath];
}
// if a download is deferred or in progress, return a placeholder image
cell.imageView.image = [UIImage imageNamed:@"Placeholder.png"];
}
else
{
cell.imageView.image = appRecord.appIcon;
}
}
return cell;
}
所以 - 我實現蘋果的LazyTableImages系統有一個重要的缺陷:它開始於立即將所有圖像的所有下載。現在,如果我刪除此行:
//[self startIconDownload:appRecord forIndexPath:indexPath];
然後系統的行爲完全像你所期望的:新的圖像加載它們的佔位符滾動到視圖。但是,初始視圖單元格不會在單元格渲染器中沒有提示的情況下自動加載它們的圖像。所以,我遇到了一個問題:使用單元格渲染器中的提示,所有圖像立即加載。沒有提示,初始視圖不會加載。現在,這在Apple示例代碼中運行良好,這讓我想知道我的情況。這幾乎就像在前面建立所有的單元格,而不是顯示在顯示器中的8個左右。所以,我開始研究它,事實的確如此......我的桌子正在建造250個獨特的細胞!我不認爲UITableView是這樣工作的,我想我認爲它只構建了填充表格所需的項目。這是事實嗎?還是說它會在前面建立所有250個單元?我試圖將我的實現與Apple LazyTableImages示例進行比較,但發現NSLog似乎在Apple示例代碼中被禁用(這使得直接行爲比較非常困難)。這只是一個簡單的發佈設置,或者蘋果以某種方式鎖定了他們的示例,以便在運行時不能記錄輸出?
謝謝!
嘿嘿,很高興你把它排序。 – hooleyhoop 2010-06-07 19:06:20