Array displaying repeated object in table view cell

☆樱花仙子☆ 提交于 2020-01-06 19:46:06

问题


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


回答1:


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;
}


来源:https://stackoverflow.com/questions/10630755/array-displaying-repeated-object-in-table-view-cell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!