iPhone UITableViewCell 性能缓慢

问题描述 投票:0回答:2

我刚刚编写了一个小应用程序,它从站点提要中读取并显示在 UITableViewCell 中。我正在使用自定义视图单元,并且我的 UITableView 滚动时很困难,就像上下滚动时不太平滑一样。任何想法?这是我的 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;
}
performance uitableview custom-cell
2个回答
3
投票

在绘制单元格时进行同步下载肯定会导致滚动不顺畅。 您可以尝试用“异步”调用替换它们,并在下载发生时用通用对象填充数据。 下载完成后,然后在桌面视图上调用 reloadData。


0
投票
dequeueReusableCellWithIdentifier

方法在单元格刷新等时被调用。构建数据并在 init 中执行请求,而不是在单元格创建中!

    

© www.soinside.com 2019 - 2024. All rights reserved.