user1053651
user1053651

Reputation:

UILabel in UITableView in objective c

I want to add labels in my tableview how should I do this ?

I want to add four label in each row of tableview.. Can some one explain me how to write code for this.

Upvotes: 2

Views: 3504

Answers (5)

Kristopher Johnson
Kristopher Johnson

Reputation: 82535

What you want is a "custom UITableViewCell". If you Google for that, you'll find a lot of tutorials and information.

For example, here is a tutorial: http://www.galloway.me.uk/tutorials/custom-uitableviewcell/

Upvotes: 2

Rajesh
Rajesh

Reputation: 784

Implement the follwoing methods,

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [self getCellContentView:CellIdentifier];
    }

    UILabel *lblManuName1 = (UILabel *)[cell viewWithTag:1];
    UILabel *lblManuName2 = (UILabel *)[cell viewWithTag:2];

    UILabel *lblCmpnyName1 = (UILabel *)[cell viewWithTag:3];
    UILabel *lblCmpnyName2  = (UILabel *)[cell viewWithTag:4];


        lblManuName1.text = @"Lbl 1" ; 
    lblManuName2.text = @"Lbl 2" ;
    lblManuName2.text = @"Lbl 3" ; 
    lblCmpnyName2.text = @"Lbl 4" ;

    return cell;
}

- (UITableViewCell *) getCellContentView:(NSString *)cellIdentifier {

    CGRect CellFrame = CGRectMake(0, 0, 250, 30);

    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];

    CGRect LabelManuName1Frame;
    CGRect LabelManuName2Frame ;

    CGRect LabelCmpnyName1Frame;
    CGRect LabelCmpnyName2Frame;

    LabelManuName1Frame = CGRectMake(10, 5, 75, 15);
    LabelManuName2Frame = CGRectMake(59, 5, 205, 15);

    LabelCmpnyName1Frame = CGRectMake(10, 22, 100, 15);
    LabelCmpnyName2Frame = CGRectMake(59, 22, 205, 15);



    if (orientation == UIInterfaceOrientationLandscapeLeft || 
        orientation == UIInterfaceOrientationLandscapeRight) {

        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
            //LabelManuName1Frame = CGRectMake(10, 13.5, 180, 15);
            //LabelManuName2Frame = CGRectMake(59, 13.5, 580, 15);
            LabelManuName1Frame = CGRectMake(10, 5, 70, 30);
            LabelManuName2Frame = CGRectMake(80, 5, 350, 30);

            //LabelCmpnyName1Frame = CGRectMake(410, 13.5, 235, 15);
            //LabelCmpnyName2Frame = CGRectMake(459, 13.5, 580, 15);
            LabelCmpnyName1Frame = CGRectMake(435, 5, 70, 30);
            LabelCmpnyName2Frame = CGRectMake(505, 5, 350, 30);

        }else{
            LabelManuName1Frame = CGRectMake(10, 5, 75, 15);
            LabelManuName2Frame = CGRectMake(59, 5, 370, 15);

            LabelCmpnyName1Frame = CGRectMake(10, 22, 130, 15);
            LabelCmpnyName2Frame = CGRectMake(59, 22, 370, 15);
        }

    } else {
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
            //LabelManuName1Frame = CGRectMake(10, 13.5, 180, 15);
            //LabelManuName2Frame = CGRectMake(59, 13.5, 300, 15);
            LabelManuName1Frame = CGRectMake(10, 5, 70, 30);
            LabelManuName2Frame = CGRectMake(80, 5, 250, 30);

            //LabelCmpnyName1Frame = CGRectMake(370, 13.5, 205, 15);
            //LabelCmpnyName2Frame = CGRectMake(419, 13.5, 200, 15);
            LabelCmpnyName1Frame = CGRectMake(330, 5, 70, 30);
            LabelCmpnyName2Frame = CGRectMake(400, 5, 250, 30);
        }else { 
            LabelManuName1Frame = CGRectMake(10, 5, 75, 15);
            LabelManuName2Frame = CGRectMake(59, 5, 205, 15);

            LabelCmpnyName1Frame = CGRectMake(10, 22, 100, 15);
            //Increse width of LabelCmpnyName2Frame
            LabelCmpnyName2Frame = CGRectMake(59, 22, 205, 15);
        }
    }



    UILabel *lblTemp;

    UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CellFrame reuseIdentifier:cellIdentifier] autorelease];

    //Initialize Label with tag 3.
    lblTemp = [[UILabel alloc] initWithFrame:LabelManuName1Frame];
    lblTemp.tag = 1;
    //lblTemp.font =  [UIFont fontNamesForFamilyName:@"Arial"];
    lblTemp.font =  [UIFont fontWithName:@"Arial-BoldMT" size:13];
    lblTemp.backgroundColor = [UIColor clearColor];
    [cell.contentView addSubview:lblTemp];
    [lblTemp release];  

    //Initialize Label with tag 4.
    lblTemp = [[UILabel alloc] initWithFrame:LabelManuName2Frame];
    lblTemp.tag = 2;
    //lblTemp.font =  [UIFont fontNamesForFamilyName:@"Arial"];
    lblTemp.font =  [UIFont fontWithName:@"Arial-BoldMT" size:13];
    lblTemp.backgroundColor = [UIColor clearColor];
    [cell.contentView addSubview:lblTemp];
    [lblTemp release];  

    //Initialize Label with tag 5.
    lblTemp = [[UILabel alloc] initWithFrame:LabelCmpnyName1Frame];
    lblTemp.tag = 3;
    //lblTemp.font =  [UIFont fontNamesForFamilyName:@"Arial"];
    lblTemp.font =  [UIFont fontWithName:@"Arial-BoldMT" size:13];
    lblTemp.backgroundColor = [UIColor clearColor];
    [cell.contentView addSubview:lblTemp];
    [lblTemp release];  

    //Initialize Label with tag 6.
    lblTemp = [[UILabel alloc] initWithFrame:LabelCmpnyName2Frame];
    lblTemp.tag = 4;
    //lblTemp.font =  [UIFont fontNamesForFamilyName:@"Arial"];
    lblTemp.font =  [UIFont fontWithName:@"Arial-BoldMT" size:13];
    lblTemp.backgroundColor = [UIColor clearColor];
    [cell.contentView addSubview:lblTemp];
    [lblTemp release];

    return cell;
}

Upvotes: 2

Prashant Shukla
Prashant Shukla

Reputation: 264

You can do this thing easily with UITableViewCell. Mmake one custom cell add number of labels you want. Add that custom cell in your tableview. See the follwoing code:

@interface EditingTableCell : UITableViewCell 
{  
    UILabel       *status;
    UILabel       *valueofplayername;
}

@property (nonatomic,retain) IBOutlet UILabel *status;
@property (nonatomic,retain) IBOutlet UILabel *valueofplayername;

Take one uitableviewcell from IB add your labels and link with your IBOutlet. After this add this code in your cellForRowAtIndexPath delegate:

EditingTableCell *cell = (EditingTableCell *)[tableView dequeueReusableCellWithIdentifier:IngredientsCellIdentifier];
if (cell == nil)
{
    [[NSBundle mainBundle] loadNibNamed:@"EditingTableCell" owner:self options:nil];
    cell = edittableviewcell;
    self.edittableviewcell = nil;
}

Check this link : http://www.icodeblog.com/2009/05/24/custom-uitableviewcell-using-interface-builder/

Upvotes: 1

Stuart
Stuart

Reputation: 37043

UITableViewCell objects have a contentView property. Add any custom views as subviews of contentView.

Upvotes: 2

Antwan van Houdt
Antwan van Houdt

Reputation: 6991

your tableview row is a view, you could simply add some labels as a subview, although I don't know whether this would be the best thing to do anyways.

What are you trying to accomplish? 4 Labels seems a bit much for the small screen the iPhone has.

Upvotes: 0

Related Questions