0

I have custom cell in my UITableView and according to the string's value I want to add a UILabel in the cell.

Here is my code for cell,

   -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSDictionary * tmpDictn = [tableAry objectAtIndex:indexPath.section];
        NSString * typeStr = [tmpDictn objectForKey:@“DocumentType”];

        NSString * cellIdentifier = @"TestCell";

        TestCell *cell = (TestCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

        if (!cell){
            cell = [[TestCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        }

        if ([typeStr isEqualToString:@“Text”]) {
                    UILabel * textLbl = [[UILabel alloc] init];
                    textLbl.backgroundColor=[UIColor clearColor];
                    textLbl.textColor=[UIColor lightGrayColor];
                    textLbl.userInteractionEnabled=NO;
                    textLbl.numberOfLines = 0;
                    textLbl.font = [UIFont fontWithName:@“Helvetica" size:16];
                    [textLbl setFrame:CGRectMake(30, 20, 250, 25)];
                textLbl.text= [NSString stringWithFormat:@"%@ %i",[splitAry objectAtIndex:i],indexPath.section];
                    [cell addSubview:textLbl];
            }

        }

        return cell;
    }

My UITableView contain 5 cell(dynamic). And only first cell should have this label(this also change according to Text). This code is adding UILabel in first cell but also add UILabel at 3 and 5th cell.

I have checked that its same UILabel created 1 time and added in cell 1st, 3rd and 5th. And "Text" is only at first position in Tableary.

Darshan Kunjadiya
  • 3,323
  • 1
  • 29
  • 31
  • is the typStr value is constant or dynamic – Suhail kalathil Jul 01 '14 at 08:05
  • 1
    You are using dequeueReusableCellWithIdentifier therefore once you will add label to cell it will be there for next time when it's reused. You are not removing label anywhere just adding. I would suggest to subclass UITableViewCell, create your own custom cell and configure it as needed. – ppolak Jul 01 '14 at 08:12
  • If you will scroll the table view, label will change its position. Reason is you are using same cell identifier for all cells in case of cell reusability. Use different cell identifier for those cell, that you want with label, and use different for the rest. – Yogendra Jul 01 '14 at 08:48

3 Answers3

1

Try using a different cell identifier for the cells that need a label added to them, e.g. @"TextCell". Otherwise, you are reusing cells that already have a label added even if it is not supposed to be there. Alternatively, you could remove the label (if it is there) in an 'else' condition of your if ([typeStr isEqualToString:@“Text”]) block but I think that the former is cleaner.

F1ank3r
  • 199
  • 1
  • 11
1
  -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSDictionary * tmpDictn = [tableAry objectAtIndex:indexPath.section];
        NSString * typeStr = [tmpDictn objectForKey:@“DocumentType”];

        NSString * cellIdentifier = @"TestCell";

        TestCell *cell = (TestCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

        if (!cell){
            cell = [[TestCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        }
          UILabel *lbl = [cell viewWithTag:1];
           if(lbl)
           {
              [lbl removeFromSuperView];
           }
        if ([typeStr isEqualToString:@“Text”]) {

                    UILabel * textLbl = [[UILabel alloc] init];
                    textLabel.tag = 1;
                    textLbl.backgroundColor=[UIColor clearColor];
                    textLbl.textColor=[UIColor lightGrayColor];
                    textLbl.userInteractionEnabled=NO;
                    textLbl.numberOfLines = 0;
                    textLbl.font = [UIFont fontWithName:@“Helvetica" size:16];
                    [textLbl setFrame:CGRectMake(30, 20, 250, 25)];
                textLbl.text= [NSString stringWithFormat:@"%@ %i",[splitAry objectAtIndex:i],indexPath.section];
                    [cell addSubview:textLbl];
            }

        }

        return cell;
}
Suhail kalathil
  • 2,673
  • 1
  • 13
  • 12
0

You are using

[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

The problem for your code is that table view is reusing your cell. So it will appear in many as your number of cells increases.

I am a bit old programmer but I think the best way is to

if (!cell) {

cell = [[TestCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

        //Add your label here.

}
if(!label) { //your code;

//set label text to nil if your condition not met;

}

A quick solution for you is to put an else and set text to nil;

Cheers.

Nikhil.T
  • 241
  • 2
  • 13