开发者

iPhone UITableViewCell slow performance

开发者 https://www.devze.com 2022-12-11 08:58 出处:网络
I just wrote a small application that read from a site feed and display in UITableViewCell. I am using custom view cell and my UITableView is screwed in scrolling like it is not very smooth in scrolli

I just wrote a small application that read from a site feed and display in UITableViewCell. I am using custom view cell and my UITableView is screwed in scrolling like it is not very smooth in scrolling upside down. Any idea? Here's the code for my UITableViewCell,

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

    static NSString *CellIdentifier = @开发者_JS百科"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;
}


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.


afaik the dequeueReusableCellWithIdentifier method is called as cells get flush etc. Build your data / do the requests on init, not in the cell creation!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号