【问题标题】:UIimage from server through JSON loads slow in uitableview来自服务器的 UIimage 通过 JSON 在 uitableview 中加载缓慢
【发布时间】:2012-04-09 15:27:30
【问题描述】:
 The uitableview view gets slow while receiving images from server through Json

` -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSString* CellIdentifier = [NSString stringWithFormat:@"Cell%d",indexPath.row];
    UITableViewCellFixed *cell = (UITableViewCellFixed *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[[UITableViewCellFixed alloc] initWithStyle:UITableViewCellStyleSubtitle 
                                            reuseIdentifier:CellIdentifier] autorelease];;
    }

   NSURL *url = [NSURL URLWithString:[dict objectForKey:@"allfeeds3"]];
    NSData *data = [[NSData alloc] initWithContentsOfURL:url];
 // cell.imageView.image = [UIImage imageWithData:data];
    UIImage *tmpImage = [[UIImage alloc] initWithData:data];
    cell.imageView.image = tmpImage;

}

【问题讨论】:

标签: iphone uitableview


【解决方案1】:

我确信以前也有人问过同样的问题,而且很可能是你问过的,因为代码非常相似。

每次数据源向其委托请求一个单元格时,您都会创建一个新单元格。这行代码:

 NSString* CellIdentifier = [NSString stringWithFormat:@"Cell%d",indexPath.row];

每次都会创建一个新单元格。如果每个单元格都相同(在布局方面,而不是内容方面),则使用单个静态 NSString 作为重用标识符。这样,tableview 将尝试从其队列中拉出一个可重用的单元格,而不是每次都创建一个。

此外,您似乎只是想在主线程上下载数据。并且即使单元格确实被重用,您也分配/初始化一个新的 UIImageView。您需要继承 UITableViewCell 并在 layoutSubviews 中设置 UIImageView,或者从 nib 加载它。需要重新考虑您对此的整个方法。小心点。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-14
    • 2021-07-26
    • 2016-06-11
    • 1970-01-01
    • 2023-01-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多