iPhone的UITableViewCell性能下降
问题描述:
我刚写的是从网站饲料和显示的UITableViewCell读一个小应用程序。我正在使用自定义视图单元格,并且我的UITableView在滚动中旋转,就像它在上下滚动时不太平滑一样。任何想法?下面是我的UITableViewCell代码,iPhone的UITableViewCell性能下降
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"CustomCell";
CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
// cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:nil options:nil];
for(id currentObject in topLevelObjects) {
if([currentObject isKindOfClass:[UITableViewCell class]]) {
cell = (CustomCell *) currentObject;
break;
}
}
}
//MiceAppDelegate *AppDelegate = (MiceAppDelegate *)[[UIApplication sharedApplication] delegate];
if(dataArray != nil) {
//
//NSArray *promoArray = (NSArray *) promotions;
NSDictionary *datadict = [self.dataArray objectAtIndex:indexPath.row];
NSString *url = [datadict objectForKey:@"imageUrl"];
NSString *title = [datadict objectForKey:@"title"];
NSString *description = [datadict objectForKey:@"description"];
NSString *newAddressPartOfURL = [url stringByReplacingOccurrencesOfString:@" " withString:@"+"];
//NSLog([url stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]);
NSURLResponse *urlResponse;
NSData *data = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:newAddressPartOfURL]] returningResponse:&urlResponse error:nil];
// Configure the cell.
UIImage *urlImage = [[UIImage alloc] initWithData:data];
// NSLog(@"%i",[imageView.image topCapHeight]);
cell.title.text = title;
cell.description.text = description;
cell.image.image = urlImage;
[urlImage release];
}
return cell;
}
答
AFAIK的dequeueReusableCellWithIdentifier方法称为细胞得到冲洗等建立你的数据/做初始化请求,而不是在细胞的创造!
答
做同步下载作为你的细胞都被吸收是肯定会引起一些不光滑滚动。您可以尝试用asynchronous调用替换那些调用,并在下载过程中用普通对象填充数据。下载完成后,在你的tableview上调用reloadData。