向上和向下滚动几次后,TableView会变慢

时间:2013-11-22 10:40:11

标签: ios objective-c uitableview

我在xcode中有一个tableview,目前从parse.com后端下载并显示在表中。它将下载一个小图像并将图像渲染成一个圆圈,并为每一行设置样式。目前它正在下载大约5行并输出可爱但我只要向上和向下滚动几次tableview和app就会变慢。

做一些研究我可以看到在另一个线程上添加数据获取的建议。目前我对这一切都不熟悉,我想知道是否有人可以帮助向我展示它是如何完成的。

我的CellForRowAtIndexPath如下所示:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

tableView.separatorColor = [UIColor colorWithRed:192.0/255.0 green:196.0/255.0 blue:202.0/255.0 alpha:1];

if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
    [self.tableView setSeparatorInset:UIEdgeInsetsZero];
}


if (cell == nil)
{
    cell = [[UITableViewCell alloc]
            initWithStyle:UITableViewCellStyleDefault
            reuseIdentifier:CellIdentifier];
}



PFObject *games = [self.games objectAtIndex:indexPath.row];

//PFFile *imageFile = [games objectForKey:@"gameCover"];


NSString *gameName = [games objectForKey:@"name"];
NSArray *gamePlatforms = [games objectForKey:@"platform"];

NSString *platformsResult = [gamePlatforms componentsJoinedByString:@""];



NSDate *rDate = [games objectForKey:@"release_date"];



// Check to see if the date is set to a Quarter instead
if (rDate == (id)[NSNull null]) {

    self.releaseDate = @"Q1/2014";

} else {

    // Date on the right added to a subview

    //Setup date

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    //uncomment to get the time only
    //[formatter setDateFormat:@"hh:mm a"];
    [formatter setDateFormat:@"dd"];
    //[formatter setDateStyle:NSDateFormatterLongStyle];


    //get the date today


    NSDate *start = [NSDate date];

    NSDateFormatter *f = [[NSDateFormatter alloc] init];
    [f setDateFormat:@"YYYY-MM-dd"];

    NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components = [gregorianCalendar components:NSDayCalendarUnit
                                                        fromDate:start
                                                          toDate:rDate
                                                         options:0];

    //NSLog(@"%ld", (long)[components day]);

    NSInteger value = [components day];


    NSString *myString = [NSString stringWithFormat:@"%0.0f days", (float)value];


    if ([myString  isEqual: @"1 days"])
    {
        myString = @"1 day";
    }

    self.releaseDate = myString;


}





// Cell Style
// Background Color

UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 15, 300.0, 100.0)];
imageView.userInteractionEnabled = NO;
imageView.backgroundColor = [UIColor colorWithRed:209.0/255.0 green:230.0/255.0 blue:244.0/255.0 alpha:1];

// Icon Image



//Game Title Label
UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(80, 25, 200, 20)];
titleLabel.text = gameName;
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.font = [UIFont fontWithName:@"ProximaNova-Light" size:13];
titleLabel.textColor = [UIColor colorWithRed:44.0/255.0 green:127.0/255.0 blue:178.0/255.0 alpha:1];

// Game Date Label

UILabel *dateLabel = [[UILabel alloc]initWithFrame:CGRectMake(250, 80, 50, 20)];
dateLabel.text = self.releaseDate;
dateLabel.backgroundColor = [UIColor clearColor];
dateLabel.font = [UIFont fontWithName:@"ProximaNova-Light" size:13];
dateLabel.textColor = [UIColor colorWithRed:44.0/255.0 green:127.0/255.0 blue:178.0/255.0 alpha:1];
dateLabel.textAlignment = NSTextAlignmentRight;

// Game Platforms

UILabel *platformLabel = [[UILabel alloc]initWithFrame:CGRectMake(250, 25, 50, 20)];
platformLabel.text = platformsResult;
platformLabel.backgroundColor = [UIColor clearColor];
platformLabel.font = [UIFont fontWithName:@"ProximaNova-Light" size:13];
platformLabel.textColor = [UIColor colorWithRed:44.0/255.0 green:127.0/255.0 blue:178.0/255.0 alpha:1];
platformLabel.textAlignment = NSTextAlignmentRight;

// add the styles to the subview

[cell addSubview:imageView];
//[cell addSubview:iconView];
[cell addSubview:titleLabel];
[cell addSubview:dateLabel];
[cell addSubview:platformLabel];

PFFile *imageFile = [games objectForKey:@"gameCover"];

// If no image

if (imageFile != NULL) {
    NSURL *imageFileUrl = [[NSURL alloc] initWithString:imageFile.url];
    NSData *imageData = [NSData dataWithContentsOfURL:imageFileUrl];
    UIImage *myImage = [UIImage imageWithData:imageData];

    UIImageView *iconView = [[UIImageView alloc] initWithImage:myImage];
    iconView.frame = CGRectMake(20.0, 25.0, 50.0, 50.0);

    iconView.layer.masksToBounds = YES;
    iconView.layer.cornerRadius = 25.0f;

    [cell addSubview:iconView];
} else {

    UIImageView *iconView = [[UIImageView alloc]initWithFrame:CGRectMake(20.0, 25.0, 50.0, 50.0)];
    iconView.userInteractionEnabled = NO;
    iconView.image = [UIImage imageNamed:@"img.png"];

    iconView.layer.masksToBounds = YES;
    iconView.layer.cornerRadius = 25.0f;

    [cell addSubview:iconView];
}

return cell;   
}

对不起,我可以发布更多代码,如果有人愿意的话。

由于

5 个答案:

答案 0 :(得分:1)

因为您正在cellForRowAtIndexPath下载图片。您必须异步下载图像。因为这种应用性能会提高。所以尝试异步下载。

答案 1 :(得分:1)

嗯,U R不断向UITableViewCell添加视图:

[cell addSubview:imageView];
//[cell addSubview:iconView];
[cell addSubview:titleLabel];
[cell addSubview:dateLabel];
[cell addSubview:platformLabel];

如果(cell == nil)阻塞导致R可重复使用的单元格,则只应添加一次。

这样做:

if (cell == nil)
{
    cell = [[UITableViewCell alloc]
            initWithStyle:UITableViewCellStyleDefault
            reuseIdentifier:CellIdentifier];


// Cell Style
// Background Color

UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 15, 300.0, 100.0)];

[imageView setTag:100];
imageView.userInteractionEnabled = NO;
imageView.backgroundColor = [UIColor colorWithRed:209.0/255.0 green:230.0/255.0 blue:244.0/255.0 alpha:1];

//Game Title Label
UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(80, 25, 200, 20)];
[titleLabel setTag:101];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.font = [UIFont fontWithName:@"ProximaNova-Light" size:13];
titleLabel.textColor = [UIColor colorWithRed:44.0/255.0 green:127.0/255.0 blue:178.0/255.0 alpha:1];

// Game Date Label

UILabel *dateLabel = [[UILabel alloc]initWithFrame:CGRectMake(250, 80, 50, 20)];
[dateLabel setTag:102];
dateLabel.backgroundColor = [UIColor clearColor];
dateLabel.font = [UIFont fontWithName:@"ProximaNova-Light" size:13];
dateLabel.textColor = [UIColor colorWithRed:44.0/255.0 green:127.0/255.0 blue:178.0/255.0 alpha:1];
dateLabel.textAlignment = NSTextAlignmentRight;

// Game Platforms

UILabel *platformLabel = [[UILabel alloc]initWithFrame:CGRectMake(250, 25, 50, 20)];
[platformLabel setTag:103];
platformLabel.text = platformsResult;
platformLabel.backgroundColor = [UIColor clearColor];
platformLabel.font = [UIFont fontWithName:@"ProximaNova-Light" size:13];
platformLabel.textColor = [UIColor colorWithRed:44.0/255.0 green:127.0/255.0 blue:178.0/255.0 alpha:1];
platformLabel.textAlignment = NSTextAlignmentRight;

// add the styles to the subview

[cell addSubview:imageView];
//[cell addSubview:iconView];
[cell addSubview:titleLabel];
[cell addSubview:dateLabel];
[cell addSubview:platformLabel];

......
}

UILabel *lbl = [cell viewWithTag:101];
lbl.text = gameName;

lbl = [cell viewWithTag:102];
lbl.text = self.releaseDate;

lbl = [cell viewWithTag:103];
lbl.text = platformsResult;

为您的ImageViews做同样的事情,U将进入可重用性和快速表的世界:)

而且U也不应该下载这样的图像,U应该像其他人推荐的那样使用异步方法

答案 2 :(得分:0)

使用此代码在cellForRowAtIndexPath

中异步下载图片
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
        dispatch_async(queue, ^(void) {

            NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:parsedData[@"imageLR"]];

            UIImage* image = [[UIImage alloc] initWithData:imageData];
            if (image) {
                 dispatch_async(dispatch_get_main_queue(), ^{
                     if (cell.tag == indexPath.row) {
                         cell.imageView.image = image;
                         [cell setNeedsLayout];
                     }
                 });
             }
        });

答案 3 :(得分:0)

如果没有这个图像,则替换代码  //如果没有图像

if (imageFile != NULL) {
    NSURL *imageFileUrl = [[NSURL alloc] initWithString:imageFile.url];
dispatch_queue_t callerQueue = dispatch_get_current_queue();
dispatch_queue_t downloadQueue = dispatch_queue_create("Image Downloader", NULL);
dispatch_async(downloadQueue, ^{
    NSData *imageData = [NSData dataWithContentsOfURL:imageFileUrl];
    dispatch_async(callerQueue, ^{
        UIImage *myImage = [UIImage imageWithData:imageData];

    UIImageView *iconView = [[UIImageView alloc] initWithImage:myImage];
    iconView.frame = CGRectMake(20.0, 25.0, 50.0, 50.0);

    iconView.layer.masksToBounds = YES;
    iconView.layer.cornerRadius = 25.0f;

    [cell addSubview:iconView];
    });
});
}

答案 4 :(得分:0)

用户SDWebImageCache

此类通过此类在后台下载图像,并在此类中处理图像缓存,并且您的表格将滚动顺畅关注此链接Here