简体   繁体   中英

Problem in UITableView custom cell

I am trying to add custom labels to my cell in UITableView. When I try to do this the display is messed up and I am unable to figure out what exactly is going on. Please find the image below and I will post the method I wrote..

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


    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    //cell.textLabel.font=[UIFont systemFontOfSize:16];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    NSString *contact=[contactKeys objectAtIndex:[indexPath section]];
    NSArray *contactSection=[contactNames objectForKey:contact];
    NSMutableArray *sugar=[db sugarId];

    if (isSearchOn) { 
        NSString *cellValue = [searchResult objectAtIndex:indexPath.row]; 
        NSArray *splitText = [cellValue componentsSeparatedByString:@":"];
        NSString *contactText = [NSString stringWithFormat:@"%@ %@", [splitText objectAtIndex:1], [splitText objectAtIndex:0]];
        //NSString *result=[contactText stringByAppendingString:[sugar objectAtIndex:[indexPath row]]];
        firstLabel=[[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 300, 40)]autorelease];                                                  
        firstLabel.tag =40; //This should be a constant probably
        firstLabel.font = [UIFont systemFontOfSize:[UIFont labelFontSize]];
        firstLabel.text = contactText;
        [firstLabel sizeToFit];
        [cell.contentView addSubview:firstLabel];

        sugarLabel = [[UILabel alloc] initWithFrame:
                      CGRectMake(10+firstLabel.frame.size.width+2, 10, 300, 60)];
        sugarLabel.tag =40; //This should be a constant probably
        //sugarLabel.font = [UIFont boldSystemFontOfSize:16];
        sugarLabel.text = [sugar objectAtIndex:[indexPath row]];
        [sugarLabel setHidden:YES];        
        [cell.contentView addSubview:sugarLabel];


        cell.textLabel.text =firstLabel.text;
        } else {
         NSString *cellText = [contactSection objectAtIndex:[indexPath row]];

            // split the text by the : to get an array containing { "AAA", "BBB" }
            NSArray *splitText = [cellText componentsSeparatedByString:@":"];

            // form a new string of the form "BBB AAA" by using the individual entries in the array
            NSString *contactText = [NSString stringWithFormat:@"%@ %@", [splitText objectAtIndex:1], [splitText objectAtIndex:0]];

            firstLabel=[[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 300, 40)]autorelease];                                                  
            firstLabel.tag =40; //This should be a constant probably
            //firstLabel.font = [UIFont systemFontOfSize:[UIFont labelFontSize]];
            firstLabel.text = contactText;
            [firstLabel sizeToFit];
            [cell.contentView addSubview:firstLabel];

            sugarLabel = [[UILabel alloc] initWithFrame:
                                    CGRectMake(10+firstLabel.frame.size.width+2, 10, 300, 60)];
            sugarLabel.tag =40; //This should be a constant probably
            //sugarLabel.font = [UIFont boldSystemFontOfSize:16];
            sugarLabel.text = [sugar objectAtIndex:[indexPath row]];
            [sugarLabel setHidden:YES];        
            [cell.contentView addSubview:sugarLabel];

            NSString *result=[NSString stringWithFormat:@"%@ %@",firstLabel.text,sugarLabel.text];
            cell.textLabel.text=result;

    }

替代文字

Figure 2: This is modification to the code as suggested by BP. The result is here under.. 替代文字

EDIT:

Whenever I try to write stringWithFormat or the above statement, the memory is not freed and the labels are getting overlap over one other. Is there a way to solve this problem..please help me..I spent more than half a day trying to figure this but no luck

If you want to have custom cells, I recommend you subclass UITableViewCell. In the example code you have given, you are creating a new label every single time the tableview asks for the cell for a row at an index path. While you are autoreleasing them, you are also adding them the the contentview of the cell, which increases their retain count and ensures that their lifetime is as long as the tableview's.

As cells are reused, you will continue to add more and more labels to it's content view, until you eventually run out of memory.

As far as the strange text output, it looks like you are adding a UILabel to one of your string with format calls.

If you were to do something like the code below, you would see something similar.

UILabel *label = [[UILabel alloc] init];
NSLog(@"%@", label);
[label release];

Edit: Seeing your edited screenshot, I'm assuming you scrolled down to the 'S' section. In which case, you are seeing the labels on top of other labels. Like I mentioned, you are creating a new label every time and putting it on top of the label you created the first (and second, third, etc) time you used the cell.

The reason you are getting the UILabel:... items showing up is the last line of the method, where you set the cell.textLabel.text. You don't want to do this if you are creating the cell with your own fields inside it.

NSString *result=[NSString stringWithFormat:@"%@ %@",contactText,sugarLabel]

should read:

NSString *result=[NSString stringWithFormat:@"%@ %@",contactText,sugarLabel.text]

You are passing in an UILabel and not a NSString

edit

Try to instantiate and add UILabels only in the if (cell == nil) { block.

while accessing labels properties and writing to it outside this block is ok

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM