GauravBoss
GauravBoss

Reputation: 140

multiple labels in table cell...iphone

i am new programmer of iphone App... i have 7 labels and 1 imageView in first cell of table view......

i write this code for that..... this work satisfactory...(may be it take times when scroll)

please tell me ...this is the right way to do this task or not....?

if not please...tell me ..the right way...

thanks in advance

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"Cell %i",indexPath.section]];

if (cell == nil) {

    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:[NSString stringWithFormat:@"Cell %i",indexPath.section]] autorelease];

}


if(indexPath.row==0)
{


            CGRect frame=CGRectMake(120,10, 80, 40);

            UILabel *label1=[[UILabel alloc]init];

            label1.frame=frame;

    label1.text=@"first label";

    [cell.contentView addSubview:label1];

            [label1 release];




    CGRect frame2=CGRectMake(200,10, 80, 40);

            UILabel *label2=[[UILabel alloc]init];

            label2.frame=frame2;

    label2.text=@"second label";

    [cell.contentView addSubview:label2];

            [label2 release];


    and so on.......


}
    else if(indexPath.row==1)
    {
       //add four labels for this cell here......
    }


return cell;
}

Upvotes: 0

Views: 4001

Answers (2)

Denis
Denis

Reputation: 6413

I think, it will be much easier to design such a complicated cell in InterfaceBuilder. If you're using Storyboards, you can design custom cells right away in your table view. If you're using xibs, you can create a nib that will have a custom UITableViewCell instead of the table view, UIViewController as owner and declare subclass of UITableViewCell in your project. That should make your sufferings much easier =)

Upvotes: 0

Nekto
Nekto

Reputation: 17877

When you are reusing cell you don't need to create labels second time:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"Cell %i",indexPath.section]];

    if (cell == nil) {

        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:[NSString stringWithFormat:@"Cell %i",indexPath.section]] autorelease];

        if(indexPath.row==0)
        {
            CGRect frame=CGRectMake(120,10, 80, 40);
            UILabel *label1=[[UILabel alloc]init];            
            label1.frame=frame;
            label1.text=@"first label";
            label1.tag = 1001;
            [cell.contentView addSubview:label1];
            [label1 release];

            CGRect frame2=CGRectMake(200,10, 80, 40);
            UILabel *label2=[[UILabel alloc]init];
            label2.frame=frame2;
            label2.text=@"second label";
            label2.tag = 1002;
            [cell.contentView addSubview:label2];
            [label2 release];

            and so on.......
        }
    }


    if(indexPath.row==0)
    {
        UILabel *label1=[cell viewWithTag:1001];
        label1.text=@"first label";

        UILabel *label2=[cell viewWithTag:1002];
        label2.text=@"second label";

        and so on.......
    }


    return cell;
}

I'm accessing previously created labels using there tag value.

Upvotes: 3

Related Questions