nessence有很多很好的信息。更多的想法:
你在這裏瘋狂地泄漏。你創建的每個單元格都會泄漏2個UILabels,一個UIImageView和一個UIImage。
如前所述,您不僅會泄漏這些內容,而且還會因爲您使用addSubview將其中一個放在另一個之上而積累在視圖中。
在單元格繪製期間接觸到網絡非常慢,並且阻止了您的用戶界面。如果這些URL是本地的,那麼您可以使用UIImage的+ imageWithContentsOfFile,如果不是,則需要在後臺加載。
我不認爲你需要一個線程在這裏。 NSURLConnection是一種在後臺加載數據而不會產生線程開銷的好方法。
nessence是完全正確的,你需要一個Story的模型類。
您可重用單元配置的基本方法不正確。您不會獲取可重用的單元格,然後向其添加子視圖。所有的子視圖都應該添加到if()塊中以創建單元格。然後,在每一遍中,你只需改變事物的價值。我已經重寫了下面的一些代碼來演示。這仍然是不正確的代碼,因爲它在單元格繪製期間觸及網絡,這可能是太多元素在子視圖單元格中(而不是自定義單元格),但它更接近正確的想法。我甚至不知道這是否編譯;我只是在這裏輸入。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = @"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
// Here we do all our cell creation
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
// Make the label
CGRect aframe = CGRectMake(80, 30, 250, 40);
UILabel *textLabel = [[[UILabel alloc] initWithFrame:aframe] autorelease]; // Note the -autorelease so we don't leak
textLabel.font = [UIFont boldSystemFontOfSize:14];
textLabel.numberOfLines=0;
textLabel.textColor = [UIColor darkTextColor];
textLabel.backgroundColor = [UIColor whiteColor];
textLabel.tag = DescriptionTag; // A tag so we can find it later (you'll need a constant for this)
[cell.contentView addSubview:textLabel];
// And the second label
aframe = CGRectMake(80, 30, 250, 40);
textLabel = [[[UILabel alloc] initWithFrame:aframe] autorelease];
textLabel.font = [UIFont boldSystemFontOfSize:14];
textLabel.numberOfLines=0;
textLabel.textColor = [UIColor darkTextColor];
textLabel.backgroundColor = [UIColor whiteColor];
textLabel.tag = TitleTag;
[cell.contentView addSubview:textLabel];
// The image view
CGRect frame = CGRectMake(0, 0, 70,80);
UIImageView *topImageView = [[[UIImageView alloc] init] autorelease];
topImageView.frame = frame;
topImageView.tag = TopImageTag;
[cell.contentView addSubview:topImageView];
}
// all the above was cell creation; we do that as seldom as possible.
// Now we do cell configuration. We want this to be fast.
UILabel *descriptionLabel = (UILabel*)[cell.contentView viewWithTag:DescriptionTag];
descriptionLabel.text = itemDescription;
UILabel *titleLabel = (UILabel*)[cell.contentView viewWithTag:TitleTag];
titleLabel.text =[[stories objectAtIndex:indexPath.row] objectForKey:@"title"];
NSString *imageURLString = [m_imglinkArray objectAtIndex:storyIndex]; // You should have a model class called Story, not two arrays.
UIImage *image = [[[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageURLString]]] autorelease]; // This is still way too slow if it's a remote URL
UIImageView *imageView = (UIImageView*)[cell.contentView viewWithTag:TopImageTag];
imageView.image = image;
return cell;
}
我建議你花一些時間學習TableViewSuite,Practical Memory Management和Coding Guidelines for Cocoa。一段時間研究Cocoa的基礎知識也會很有用,因爲這裏的編碼風格表明你可能沒有堅實的基礎。雖然這是一本Mac書,但我仍然推薦Cocoa Programming for Mac OS X。如果你有興趣使用這個來學習iPhone,我已經編制了一個syllabus這可能會有所幫助。我還沒有審查過,但斯坦福的在線CS193P course看起來很有希望。
你應該改善格式的你,因爲它無法讀取的問題... – 2009-05-05 06:59:53