UILabel in Cell of UITableView

I am new to iPhone developer,

I am using UITableView, i want total 6 labels in my Cell of UITableView 3 label on L.H.S and 3 label on R.H.S

Here is my code snippet,

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 180;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

        CGRect frameL;
        frameL.origin.x = 10; 
        frameL.origin.y = 10;
        frameL.size.height = 50;
        frameL.size.width = 200;

        CGRect frameR;
        frameR.origin.x = 200; 
        frameR.origin.y = 10;
        frameR.size.height = 40;
        frameR.size.width = 180;


        UILabel *AlertNameLHS = [[UILabel alloc] initWithFrame:frameL];
        AlertNameLHS.font=[UIFont systemFontOfSize:16.0];
        AlertNameLHS.backgroundColor=[UIColor clearColor];
        AlertNameLHS.textColor=[UIColor redColor];
        AlertNameLHS.text=@"Alert Name :";
        [cell.contentView addSubview:AlertNameLHS];

        frameL.origin.y += 60;
        UILabel *AlertMonthLHS = [[UILabel alloc] initWithFrame:frameL];
        AlertMonthLHS.font=[UIFont systemFontOfSize:16.0];
        AlertMonthLHS.backgroundColor=[UIColor clearColor];
        AlertMonthLHS.textColor=[UIColor redColor];
        AlertNameLHS.text=@"Alert Month :";
        [cell.contentView addSubview:AlertMonthLHS];

         frameL.origin.y += 120;
         UILabel *DueOnLHS = [[UILabel alloc] initWithFrame:frameL];
         DueOnLHS.font=[UIFont systemFontOfSize:16.0];
        AlertNameLHS.text=@"Due On :";
         [cell.contentView addSubview:DueOnLHS];

    AlertNameRHS = [[UILabel alloc] initWithFrame:frameR];
    AlertNameRHS.backgroundColor=[UIColor greenColor];
    AlertNameRHS.textColor=[UIColor redColor];
    AlertNameRHS.textColor=[UIColor redColor];
    AlertNameRHS.font=[UIFont systemFontOfSize:18.0];
    [cell.contentView addSubview:AlertNameRHS];

     frameL.origin.y += 80;
    AlertMonthRHS = [[UILabel alloc] initWithFrame:frameR];
    AlertMonthRHS.font=[UIFont systemFontOfSize:18.0];
    [cell.contentView addSubview:AlertMonthRHS];

     frameL.origin.y += 120;
    DueOnRHS = [[UILabel alloc] initWithFrame:frameR];
    DueOnRHS.font=[UIFont systemFontOfSize:18.0];
    [cell.contentView addSubview:DueOnRHS];
    }

    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

    AlertNameRHS.text = [Myarray objectAtIndex:indexPath.row];

    return cell;
}

but i am unable to see my UILabel properly.

Any help will be appreciated.

EDIT: :

Answers


See the corrected code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

        CGRect frameL;
        frameL.origin.x = 10; 
        frameL.origin.y = 10;
        frameL.size.height = 50;
        frameL.size.width = 200;

        CGRect frameR;
        frameR.origin.x = 200; 
        frameR.origin.y = 10;
        frameR.size.height = 40;
        frameR.size.width = 180;


        UILabel *AlertNameLHS = [[UILabel alloc] initWithFrame:frameL];
        AlertNameLHS.font=[UIFont systemFontOfSize:16.0];
        AlertNameLHS.backgroundColor=[UIColor clearColor];
        AlertNameLHS.textColor=[UIColor redColor];
        AlertNameLHS.text=@"Alert Name :";
        [cell.contentView addSubview:AlertNameLHS];

        frameL.origin.y += 60;
        NSLog(@"fr %f", frameL.origin.y);
        UILabel *AlertMonthLHS = [[UILabel alloc] initWithFrame:frameL];
        AlertMonthLHS.font=[UIFont systemFontOfSize:16.0];
        AlertMonthLHS.backgroundColor=[UIColor clearColor];
        AlertMonthLHS.textColor=[UIColor redColor];
        AlertMonthLHS.text=@"Alert Month :";
        [cell.contentView addSubview:AlertMonthLHS];

        frameL.origin.y += 60;
        NSLog(@"fr %f", frameL.origin.y);
        UILabel *DueOnLHS = [[UILabel alloc] initWithFrame:frameL];
        DueOnLHS.font=[UIFont systemFontOfSize:16.0];
        DueOnLHS.text=@"Due On :";
        [cell.contentView addSubview:DueOnLHS];

        AlertNameRHS = [[UILabel alloc] initWithFrame:frameR];
        AlertNameRHS.backgroundColor=[UIColor greenColor];
        AlertNameRHS.textColor=[UIColor redColor];
        AlertNameRHS.textColor=[UIColor redColor];
        AlertNameRHS.font=[UIFont systemFontOfSize:18.0];
        AlertNameRHS.text = @"l1";
        [cell.contentView addSubview:AlertNameRHS];

        frameR.origin.y += 60;
        AlertMonthRHS = [[UILabel alloc] initWithFrame:frameR];
        AlertMonthRHS.font=[UIFont systemFontOfSize:18.0];
        AlertMonthRHS.text =@"l2";
        [cell.contentView addSubview:AlertMonthRHS];

        frameR.origin.y += 60;
        DueOnRHS = [[UILabel alloc] initWithFrame:frameR];
        DueOnRHS.font=[UIFont systemFontOfSize:18.0];
        DueOnRHS.text = @"l3";
        [cell.contentView addSubview:DueOnRHS];
    }

    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

    AlertNameRHS.text = [NSString stringWithFormat:@"%d",indexPath.row];

    return cell;
}

You've made too many mistakes:

The LHS1 is visible, but you override it's text with LHS3 text: AlertNameLHS.text=@"Due On :";

The LHS2 isn't visible because you only initialize it and add as a subview, configured LHS1 instead of it

The LHS3 is not visible because the cell height is 180 and it's y coordinate is 190, you also don't set it's text setting the LHS1 text instead.

The RHS labels frames are incorrect and out of cell frame, you are supposed to use frameR but using frameL, i also suggest you want to add 60 pixels to y coordinate on every step.


To perform this kind of work, I would suggest you to create one custom cell . (Add New file -> Objective-C class -> UItableViewCell)

Define 6 Lables with property

#import <UIKit/UIKit.h>

@interface DemoCell : UITableViewCell
{
    IBOutlet UILabel *lblOne;
}

@property (nonatomic,retain) IBOutlet UILabel *lblOne;

Now Create a new Empty View (Only nib file) name it Democell (for your convenience) Now in that Delete the view from nib file and from the Library drag and Drop a Table View Cell at the place of your View in nib file.

Now select your tableviewcell , and in its identity inspector , change its class to DemoCell So This will link your nib file with custom cell class..

Now in the nib file ,drag&drop 6 label, position them as per your requirement , and in the file owner link all the lbls with lblOne,lblTwo ,,,,....

Now your custom cell is complete. To use this cell in your table's cellforrowatindexpath

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    DemoCell *cell= (DemoCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell ==nil)
    {
        cell=[[[NSBundle mainBundle]loadNibNamed:@"DemoCell" owner:self options:nil] lastObject];
    }


cell.lblone.textlabel.text = @"lblOne";

Define 6 labels and set frames for that. Then, addSubView to the tableViewCell contentView.


Need Your Help

Multiplying a double value by 100.0 introduces rounding errors?

c# .net math

This is what I am doing, which works 99.999% of the time:

AngularJs POST request for json not working

angularjs json codeigniter http-post

Hi i tried POST json data in two ways its response in null