开发者

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 size开发者_开发问答ToFit];
            [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;

    }

Problem in UITableView custom cell

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

Problem in UITableView custom cell

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

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜