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