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;
}
4

2 回答 2

3

在绘制单元格时进行同步下载肯定会导致滚动不流畅。您可以尝试用异步调用替换那些,并在下载发生时用通用对象填充数据。下载完成后,在您的 tableview 上调用 reloadData。

于 2009-11-09T08:57:20.343 回答
0

afaik dequeueReusableCellWithIdentifier 方法被称为单元格刷新等。构建您的数据/在初始化时执行请求,而不是在单元格创建中!

于 2009-11-09T04:24:40.993 回答