Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
256 views
in Technique[技术] by (71.8m points)

iphone - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...