iPhone UITableViewCell 性能缓慢

发布于 2024-08-11 06:07:07 字数 2237 浏览 2 评论 0原文

我刚刚编写了一个小应用程序,它从站点提要中读取数据并在 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;
}

I just wrote a small application that reads from a site feed and displays data in UITableViewCell. I am using a custom view cell, and my UITableView is screwed in scrolling, like it is not very smooth in scrolling upside down. Any ideas? Here's the code for my 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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我的影子我的梦 2024-08-18 06:07:07

在绘制单元格时进行同步下载肯定会导致滚动不顺畅。您可以尝试将其替换为 异步 调用,并在下载过程中使用通用对象填充数据。下载完成后,在 tableview 上调用 reloadData。

Doing synchronous downloads as your cells are being drawn is definitely going to cause some unsmooth scrolling. You could try to replace those with asynchronous calls, and filling in the data with a generic object while the download is happening. When the download completes, then call reloadData on your tableview.

梦途 2024-08-18 06:07:07

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

As far as I know, the dequeueReusableCellWithIdentifier method is called as cells get flushed, etc. Build your data and do the requests in init, not in the cell creation!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文