Search code examples
iphoneuitableviewnsarray

Array displaying repeated object in table view cell


I am having a NSArray of size 11 as it formed below.

labels = [NSMutableArray arrayWithObjects:@"DIN #",@"Brand Name",@"Full Name", @"Strength", @"Medication Type", @"Presciption ID", @"Next Dosage", @"Required Dosage", @"ada", @"dasdada", @"dasdasad", nil];

but when i am displaying this array in tableview cell Using this code.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {static NSString *CellIdentifier = @"Cell";
  UITableViewCell *cell = [tableView dueueReusableCellWithIdentifier:CellIdentifier];   
UILabel* nameLabel = nil;

if(cell == nil) {   cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];nameLabel = [[UILabel alloc] initWithFrame:CGRectMake( 7.0, 10.0, 160.0, 44.0 )];

    nameLabel.text =[labels objectAtIndex:indexPath.row]; 


    nameLabel.lineBreakMode = UILineBreakModeWordWrap;

    nameLabel.numberOfLines =0;
    [nameLabel sizeToFit];
    CGSize expectedlabelsize = [nameLabel.text sizeWithFont:nameLabel.font constrainedToSize:nameLabel.frame.size lineBreakMode:UILineBreakModeWordWrap];
    CGRect newFrame =nameLabel.frame;
    newFrame.size.height =expectedlabelsize.height;


   [cell.contentView addSubview: nameLabel];

return cell;}

O/P is as below .

DIN

Brand Name

Full Name

Strength

Medication Type

Presciption ID

Next Dosage

Required Dosage

DIN

Brand Name

Full Name

see after required Dosage again DIN,Brand Name,Full Name showing ,which are already shown


Solution

  • Use viewWithTag method and i have used it following method. Try like this. I think it will be helpful to you.

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    
        static NSString *CellIdentifier = @"myCell";
        UILabel* nameLabel = nil;
        UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if(cell == nil)
        {
    
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    
        }
        else
        {
            UILabel *titleLbl = (UILabel *)[cell viewWithTag:1];
            [titleLbl removeFromSuperview];
        }
        nameLabel = [[UILabel alloc] initWithFrame:CGRectMake( 7.0, 10.0, 160.0, 44.0 )];
        nameLabel.text = [labels objectAtIndex:indexPath.row];
    
        nameLabel.lineBreakMode = UILineBreakModeWordWrap;
        nameLabel.tag =1;
        nameLabel.numberOfLines =0;
        [nameLabel sizeToFit];
        CGSize expectedlabelsize = [nameLabel.text sizeWithFont:nameLabel.font constrainedToSize:nameLabel.frame.size lineBreakMode:UILineBreakModeWordWrap];
        CGRect newFrame =nameLabel.frame;
        newFrame.size.height =expectedlabelsize.height;
        [cell.contentView addSubview: nameLabel];
        return cell;
    }