Domanda

Ho un UITableView che talvolta appena si blocca quando il suo stato caricato. Non ho idea perché questo accade e mi chiedevo se poteva essere il layouting delle cellule che faccio nel cellForRowAtIndexPath, perché il mio contenuto è dinamico e quindi la disposizione delle subviews deve essere adottata ogni volta.

C'è un approccio migliore per ottenere ciò che voglio? O c'è qualcos'altro di sbagliato in questo?

Ecco il codice che ho scritto:

In primo luogo il metodo che crea la cella personalizzato e aggiunge i diversi punti di vista alla vista dei contenuti:

- (UITableViewCell *)tableViewCellWithReuseIdentifier:(NSString *)identifier {


    UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];


    UILabel *titleLbl, *amountLbl, *title2Lbl, *numberLbl, *mergeLbl, *typeLbl;


    UIColor *bgColor, *blackColor, *amountColor , *title2Color;
    UIColor *clearColor = [UIColor clearColor];


    bgColor = [UIColor clearColor];
    title2Color = [UIColor colorWithRed:0.1 green:0.1 blue:0.6 alpha:1];
    amountColor = [UIColor redColor];
    blackColor = [UIColor blackColor];

    //The number
    numberLbl = [[UILabel alloc] initWithFrame:CGRectZero];
    numberLbl.tag = kNumberLblTag;
    numberLbl.font = [UIFont systemFontOfSize:12];
    numberLbl.textAlignment = UITextAlignmentLeft;
    numberLbl.textColor = blackColor;
    numberLbl.backgroundColor = clearColor;
    numberLbl.numberOfLines = 0;
    [cell.contentView addSubview:numberLbl];
    [numberLbl release];

    amountLbl = [[UILabel alloc] initWithFrame:CGRectZero];
    amountLbl.tag = kamountLblTag;
    amountLbl.font = [UIFont systemFontOfSize:12];
    amountLbl.textAlignment = UITextAlignmentLeft;
    amountLbl.textColor = amountColor;
    amountLbl.backgroundColor = clearColor;
    amountLbl.numberOfLines = 0;
    [cell.contentView addSubview:amountLbl];
    [amountLbl release];


    mergeLbl = [[UILabel alloc] initWithFrame:CGRectZero] ;
    mergeLbl.tag = kMergeLblTag;
    mergeLbl.font = [UIFont systemFontOfSize:12];
    mergeLbl.textAlignment = UITextAlignmentLeft;
    mergeLbl.textColor = blackColor;
    mergeLbl.backgroundColor = clearColor;
    mergeLbl.numberOfLines = 0;
    [cell.contentView addSubview:mergeLbl];
    [mergeLbl release];


    typeLbl = [[UILabel alloc] initWithFrame:CGRectZero];
    typeLbl.tag = kTypeLblTag;
    typeLbl.font = [UIFont systemFontOfSize:12];
    typeLbl.textAlignment = UITextAlignmentLeft;
    typeLbl.textColor = blackColor;
    typeLbl.backgroundColor = clearColor;
    typeLbl.numberOfLines = 1;
    [cell.contentView addSubview:typeLbl];
    [typeLbl release];


    titleLbl = [[UILabel alloc] initWithFrame:CGRectZero];
    titleLbl.tag = kTitleLblTag;
    titleLbl.font = [UIFont systemFontOfSize:14];
    titleLbl.textAlignment = UITextAlignmentLeft;
    titleLbl.textColor = blackColor;
    titleLbl.backgroundColor = clearColor;
    titleLbl.numberOfLines = 0;
    [cell.contentView addSubview:titleLbl];
    [titleLbl release];


    title2Lbl = [[UILabel alloc] initWithFrame:CGRectZero];
    title2Lbl.tag = ktitle2LblTag;
    title2Lbl.font = [UIFont systemFontOfSize:12];
    title2Lbl.textAlignment = UITextAlignmentLeft;
    title2Lbl.textColor = title2Color;
    title2Lbl.backgroundColor = clearColor;    
    title2Lbl.numberOfLines = 0;
    [cell.contentView addSubview:title2Lbl];
    [title2Lbl release];


    UIView *bgView = [[UIView alloc]initWithFrame:CGRectZero];
    bgView.backgroundColor = bgColor;
    cell.backgroundView = bgView;
    [bgView release];


    NSLog(@"tableViewCellWithReuseIdentifier");

    return cell;

}

E qui il metodo che ridimensiona le subviews per ogni cella:

    // Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {



    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];



    NSUInteger row = [indexPath row];
    NSUInteger section = [indexPath section];

    NSUInteger r = row+(section*100);

    if (cell == nil) {

        cell = [self tableViewCellWithReuseIdentifier: CellIdentifier];

    }

    UIColor *bgColor, *clearColor = [UIColor clearColor];





    //set the rePointer to the actual array 
    if (r < [results count]) {
        rePointer = results;
        //NSLog(@"results r: %i", r);
    }else {
        r = [indexPath row];
        rePointer = rejects;
        //NSLog(@"rejects r: %i", r);

    }



    NSString *noTxt = [[NSString alloc]initWithFormat:@"%i.", r+1];
    CGSize boundingSize = CGSizeMake(35, 100);
    UIFont *sysFont = [UIFont systemFontOfSize:12]; 
    CGSize requiredSize = [noTxt sizeWithFont:sysFont constrainedToSize:boundingSize lineBreakMode:UILineBreakModeWordWrap];
    CGFloat requiredHeight = requiredSize.height;

    UILabel *numberLbl = (UILabel*)[cell viewWithTag:kNumberLblTag];
    numberLbl.frame = CGRectMake(5, 5, 35, requiredHeight);
    numberLbl.text = noTxt;





    NSMutableString *amountTxt = [[NSMutableString alloc]initWithString:@"amount: "];
    [amountTxt appendString:[NSString stringWithFormat:@"%@", [[rePointer objectAtIndex:r] objectForKey:@"amount"]]];


    boundingSize = CGSizeMake(35, 100);
    sysFont = [UIFont systemFontOfSize:12]; 
    requiredSize = [amountTxt sizeWithFont:sysFont constrainedToSize:boundingSize lineBreakMode:UILineBreakModeWordWrap];
    requiredHeight = requiredSize.height;

    UILabel *amountLbl = (UILabel*)[cell viewWithTag:kamountLblTag];
    amountLbl.frame = CGRectMake(5, 20, 35, requiredHeight);
    amountLbl.text = amountTxt;





    if ([[[rePointer objectAtIndex:r] objectForKey:@"count"] intValue] > 0) {

        NSString *mergedTxt = [[NSString alloc]initWithString:@"Count"];
        boundingSize = CGSizeMake(35, 100);
        sysFont = [UIFont systemFontOfSize:12]; 
        requiredSize = [mergedTxt sizeWithFont:sysFont constrainedToSize:boundingSize lineBreakMode:UILineBreakModeWordWrap];
        requiredHeight = requiredSize.height;


        UILabel *mergeLbl = (UILabel*)[cell viewWithTag:kMergeLblTag];
        mergeLbl.frame = CGRectMake(5, 50, 35, requiredHeight);
        mergeLbl.text = mergedTxt;
        [mergedTxt release];

    }else {

        UILabel *mergeLbl = (UILabel*)[cell viewWithTag:kMergeLblTag];
        mergeLbl.frame = CGRectZero;
        mergeLbl.text = @"";

    }



    NSString *type;

    if (![[[rePointer objectAtIndex:r] objectForKey:@"type"] isEqualToString:@"NotSet"]) {
        if ([[[rePointer objectAtIndex:r] objectForKey:@"type"] isEqualToString:@"[XYZ]"]) {
            type = @"X";
        }else{
            int typeLength = [[[rePointer objectAtIndex:r] objectForKey:@"type"] length];
            type = [[[rePointer objectAtIndex:r] objectForKey:@"type"] substringWithRange:NSMakeRange(1, typeLength-2)];
        }


        UILabel *typeLbl = (UILabel*)[cell viewWithTag:kTypeLblTag];
        typeLbl.frame = CGRectMake(5, 65, 35, 20);
        typeLbl.text = type;

    }else {
        UILabel *typeLbl = (UILabel*)[cell viewWithTag:kTypeLblTag];
        typeLbl.frame = CGRectZero;
        typeLbl.text = @"";
    }






    NSString *titleTxt = [[NSString alloc]initWithString:[[[rePointer objectAtIndex:r] objectForKey:@"h_title"]stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]]];


    boundingSize = CGSizeMake(190, 1000);
    sysFont = [UIFont systemFontOfSize:14];

    requiredSize = [titleTxt sizeWithFont:sysFont constrainedToSize:boundingSize lineBreakMode:UILineBreakModeWordWrap];
    requiredHeight = requiredSize.height;

    CGFloat titleHeight = requiredHeight;

    UILabel *titleLbl = (UILabel*)[cell viewWithTag:kTitleLblTag];
    titleLbl.frame = CGRectMake(50, 5, 190, requiredHeight);
    titleLbl.text = titleTxt;
    [titleTxt release];





    NSString *title2Txt = [[NSString alloc]initWithString:[[[rePointer objectAtIndex:r] objectForKey:@"title2"]stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]]];

    sysFont = [UIFont systemFontOfSize:12];
    requiredSize = [title2Txt sizeWithFont:sysFont constrainedToSize:boundingSize lineBreakMode:UILineBreakModeWordWrap];
    requiredHeight = requiredSize.height;

    UILabel *title2Lbl = (UILabel*)[cell viewWithTag:ktitle2LblTag];
    title2Lbl.frame = CGRectMake(50, titleHeight + 10, 190, requiredHeight);
    title2Lbl.text = title2Txt;
    [title2Txt release];




    /*
    CGFloat height = [[[rePointer objectAtIndex:r] objectForKey:@"h_title"] sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(190, 1000) lineBreakMode:UILineBreakModeWordWrap].height;
    height += [[[rePointer objectAtIndex:r] objectForKey:@"title2"] sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:CGSizeMake(190, 1000) lineBreakMode:UILineBreakModeWordWrap].height;
    height += 20;
    */

    cell.contentView.backgroundColor = clearColor;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    NSLog(@"cellForRowAtIndexPath");




    /*
    cell.backgroundView.frame = CGRectMake(0, 0, 320, height-1);
    cell.backgroundView.backgroundColor = bgColor;
    */



    return cell;

}

E infine la heightForRowAtIndexPath:

 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {    


    NSUInteger row = [indexPath row];
    NSUInteger section = [indexPath section];
    row += section*100;

    NSUInteger rejectsIndex = [indexPath row];

    if (row < [results count]) {
        CGFloat height = [[[results objectAtIndex:row] objectForKey:@"h_title"] sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(190, 1000) lineBreakMode:UILineBreakModeWordWrap].height;
        height += [[[results objectAtIndex:row] objectForKey:@"title2"] sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:CGSizeMake(190, 1000) lineBreakMode:UILineBreakModeWordWrap].height;
        height += 20;
        return MAX(90, height);
    }else {
        CGFloat height = [[[rejects objectAtIndex:rejectsIndex] objectForKey:@"h_title"] sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(190, 1000) lineBreakMode:UILineBreakModeWordWrap].height;
        height += [[[rejects objectAtIndex:rejectsIndex] objectForKey:@"title2"] sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:CGSizeMake(190, 1000) lineBreakMode:UILineBreakModeWordWrap].height;
        height += 20;

        return MAX (90, height);
    }


}

Grazie per qualsiasi consiglio!

È stato utile?

Soluzione 3

è scoperto che si trattava di un bug di iOS.

Altri suggerimenti

Credo che il codice è eccessivamente complicato e quindi inefficiente. Forse si dovrebbe considerare l'utilizzo di una cella di vista tabella personalizzata che consente di accedere a tutti gli elementi puliti e direttamente.

Suggerisco anche di memorizzare nella cache le misure formato potenzialmente lento.

Perché si utilizza tag per identificare la visualizzazione secondaria nella cella. Essere la cura di una cosa, NON usare 0 per il numero di tag, o qualche numero di targa come lo stesso valore di tag della cella.

Il metodo viewWithTag:. Restituirà la prima visualizzazione secondaria con il numero di targa, se tag della cella è la stessa di quella che si desiderato, tornerà cellulare, non una visualizzazione secondaria

Il suggerimento di Eiko è giusto, si dovrebbe considerare l'utilizzo di una cella di vista tabella personalizzata.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top