簡體   English   中英

UITableView在滾動時崩潰(內存警告)

[英]UITableView crashes on scroll (memory warnings)

我已經制作了第一個UITableView ,但是當單元格的數量大於屏幕上可以顯示的數量時,我滾動時,由於內存不足,它會崩潰。

我實現了SDWEBIMAGE library來異步加載圖片。 並隨后緩存圖像。

如果需要更多代碼,請告訴我!

我是一個完全的新手,所以請保持溫柔:)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *myIdentifier = @"defaultcell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:myIdentifier forIndexPath:indexPath];

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

    }

    UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleWhiteLarge];
    spinner.center = CGPointMake(160, 47);
    [spinner startAnimating];
    [cell.contentView addSubview:spinner];

    //hide labels until done loading
    cell.textLabel.hidden = YES;
    cell.detailTextLabel.hidden = YES;
    cell.imageView.hidden = YES;

    UIImageView *iv = [[UIImageView alloc] initWithFrame:(CGRect){.size={80, 60}}];
    iv.contentMode = UIViewContentModeScaleAspectFill;
    iv.clipsToBounds = YES;
    iv.frame = CGRectMake(15, 17, 80, 60);

    NSString *profilePicName = [NSString stringWithFormat:@"%@%@", [self.dbhandler getPicturesPath], [[gallery objectAtIndex:indexPath.row] valueForKey: @"filename"]];

    [iv setImageWithURL:[NSURL URLWithString:profilePicName]  placeholderImage:[UIImage imageNamed:@"placeholder.png"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType){
            [spinner stopAnimating];
            cell.textLabel.hidden = NO;
            cell.detailTextLabel.hidden = NO;
            cell.imageView.hidden = NO;

            [cell.contentView addSubview:iv];
        }];

    NSString *subtitle = [NSString stringWithFormat:@"Comments:  %@ \nPosted:        %@", [[gallery objectAtIndex:indexPath.row] valueForKey:@"comments"], [[gallery objectAtIndex:indexPath.row] valueForKey:@"created_at"]];

    cell.detailTextLabel.numberOfLines = 0;
    cell.textLabel.text = [NSString stringWithFormat:@"Votes:    %@",[[gallery objectAtIndex:indexPath.row] valueForKey:@"votes"]];
    cell.detailTextLabel.text = subtitle;

    return cell;
}

更新的功能:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *myIdentifier = @"defaultcell";
    UIActivityIndicatorView *spinner;
    UIImageView *iv;

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:myIdentifier forIndexPath:indexPath];

    if(cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:myIdentifier];
        //build spinner
        spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleWhiteLarge];
        spinner.center = CGPointMake(160, 47);
        spinner.tag = 101;
        [cell.contentView addSubview:spinner];

        //build ImageView
        iv = [[UIImageView alloc] initWithFrame:(CGRect){.size={80, 60}}];
        iv.contentMode = UIViewContentModeScaleAspectFill;
        iv.clipsToBounds = YES;
        iv.tag = 102;
        iv.frame = CGRectMake(15, 17, 80, 60);
        [cell.contentView addSubview:iv];
    } else {
        spinner = (UIActivityIndicatorView*)[cell viewWithTag:101];
        iv = (UIImageView*)[cell viewWithTag:102];
    }



    [spinner startAnimating];
    //the rest goes here


    cell.textLabel.hidden = YES;
    cell.detailTextLabel.hidden = YES;
    cell.imageView.hidden = YES;

    NSString *profilePicName = [NSString stringWithFormat:@"%@%@", [self.dbhandler getPicturesPath], [[gallery objectAtIndex:indexPath.row] valueForKey: @"filename"]];
    NSLog(@"%@", profilePicName);

    [iv setImageWithURL:[NSURL URLWithString:profilePicName]  placeholderImage:[UIImage imageNamed:@"placeholder.png"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType){
        [spinner stopAnimating];
        cell.textLabel.hidden = NO;
        cell.detailTextLabel.hidden = NO;
        cell.imageView.hidden = NO;

        NSLog(@"done");
        [cell.contentView addSubview:(UIImageView *)[cell viewWithTag:102]];
    }];


    NSString *subtitle = [NSString stringWithFormat:@"Comments:  %@ \nPosted:        %@", [[gallery objectAtIndex:indexPath.row] valueForKey:@"comments"], [[gallery objectAtIndex:indexPath.row] valueForKey:@"created_at"]];

    cell.detailTextLabel.numberOfLines = 0;
    cell.textLabel.text = [NSString stringWithFormat:@"Votes:    %@",[[gallery objectAtIndex:indexPath.row] valueForKey:@"votes"]];
    cell.detailTextLabel.text = subtitle;

    return cell;
}

每次重復使用單元格時,都會將UIActivityIndi​​catorView / spinner和UIImageView / iv添加為子視圖。 更好的方法是在情節提要中使用自定義的原型單元格,或通過設置標簽來重用現有視圖。

UIActivityIndicatorView *spinner;
UIImageView *iv;

if(cell == nil){
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:myIdentifier];
    //build spinner
    spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleWhiteLarge];
    spinner.center = CGPointMake(160, 47);
    spinner.tag = 101;
    [cell.contentView addSubview:spinner];

    //build ImageView
    iv = [[UIImageView alloc] initWithFrame:(CGRect){.size={80, 60}}];
    iv.contentMode = UIViewContentModeScaleAspectFill;
    iv.clipsToBounds = YES;
    iv.tag = 102;
    iv.frame = CGRectMake(15, 17, 80, 60);
    [cell.contentView addSubview:iv];
} else {
    spinner = (UIActivityIndicatorView*)[cell.contentView viewWithTag:101];
    iv = (UIImageView*)[cell.contentView viewWithTag:102];
}

[spinner startAnimating];
//the rest goes here

您需要刪除其他addSubView調用(特別是在回調塊中)。

這應該可以幫助您入門。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM