Hardik rami
Hardik rami

Reputation: 341

Divide `UITableViewCell` into parts

I am creating an application in which I have to divide a cell into three parts. For example I have to give the title in one cell and display the corresponding data immediately below.

How can I do that?


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

{

static NSString *CellIdentifier = @"Cell";
myappAppDelegate *mydelegate=(myappAppDelegate *)[[UIApplication sharedApplication]delegate];

 database *objdata =[mydelegate.myarray objectAtIndex:indexPath.row];
 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) 
{
     cell = [[[UITableViewCell alloc] 
     initWithStyle:UITableViewCellStyleSubtitle     
     reuseIdentifier:CellIdentifier] autorelease];

    cell.accessoryType = UITableViewCellAccessoryNone;



    if (indexPath.row == 0) 
    {
        UILabel *temp = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, 200, 44)];
        temp.text =@"Main Balance";  
        temp.backgroundColor = [UIColor clearColor];
       temp.font = [UIFont systemFontOfSize:19];
        [cell addSubview:temp];

        UILabel *temp1 = [[UILabel alloc] initWithFrame:CGRectMake(240, 0, 50, 44)];
        temp1.text =@"2000";  
        temp1.backgroundColor = [UIColor clearColor];
        temp1.font = [UIFont systemFontOfSize:19];
        [cell addSubview:temp1];

    else if(indexPath.row==1)
    {
        UILabel *temp = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 108, 44)];
        temp.text =@"Income";  
        temp.backgroundColor = [UIColor clearColor];
        temp.font = [UIFont systemFontOfSize:19];
       [cell addSubview:temp];

    }

    else 
    {
       UILabel *temp = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 108, 44)];
        temp.text =[NSString stringWithFormat:@"%d",objdata.amount];       
       temp.backgroundColor = [UIColor clearColor];
        temp.font = [UIFont systemFontOfSize:19];
        [cell addSubview:temp];


    }

enter code here

On Home screen I create One Main Blaance text box and two Buttons Like Income,Expense,and One final display button. When I click Income it display another view in which Add income in TextBox and one button for Add to main balance.I type the Income Amount in that TextBox then click on Save,it will save in database I do same for Expense then I click on Add to main balance button it will be added or subtracted the amount in main balance text box which I display on home screen.then Click Final display button on home screen it will display three label Income,Expense,Main Balance..in TableView.I get the value Under Income and Expense But it display only Initial value which I entered in database At design time By Query.

My Question:- suppose I add 4 data then final display will be display all that data even If i add more data it also display in final display....I hope U can get my point..please Guide me I m so confused.

Upvotes: 4

Views: 2830

Answers (4)

user1201319
user1201319

Reputation: 5

you can add three uiviews to do

Upvotes: -2

hchouhan02
hchouhan02

Reputation: 946

Use this code it will helps you.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    static NSString *CellIdentifier = @"ShowMoreCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell = [self getCellContentView:CellIdentifier];

    UILabel *lbl1 = (UILabel *)[cell viewWithTag:1];
    [lbl1 setText:@"Label1"];
    UILabel *lbl2 = (UILabel *)[cell viewWithTag:2];
    [lbl2 setText:@"Label3"];
    UILabel *lbl3 = (UILabel *)[cell viewWithTag:3];
    [lbl3 setText:@"Label3"];
    return cell;
}

- (UITableViewCell *)getCellContentView:(NSString *)cellIdentifier
{
    UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier] autorelease];
    cell.backgroundColor=[UIColor clearColor];

    CGRect lbl1Rect = CGRectMake(20, 5, 280, 30);
    CGRect lbl2Rect = CGRectMake(20, 35, 280, 30);
    CGRect lbl3Rect = CGRectMake(20, 70, 280, 30);


    UILabel *lbl1 = [[UILabel alloc] initWithFrame:lbl1Rect];
    lbl1.tag=1;
    lbl1.font=[UIFont fontWithName:@"Helvetica" size:13];
    lbl1.backgroundColor=[UIColor clearColor];
    [cell.contentView addSubview:lbl1];
    [lbl1 release];

    UILabel *lbl2 = [[UILabel alloc] initWithFrame:lbl2Rect];
    lbl2.tag=1;
    lbl2.font=[UIFont fontWithName:@"Helvetica" size:13];
    lbl2.backgroundColor=[UIColor clearColor];
    [cell.contentView addSubview:lbl2];
    [lbl2 release];

    UILabel *lbl3 = [[UILabel alloc] initWithFrame:lbl3Rect];
    lbl3.tag=1;
    lbl3.font=[UIFont fontWithName:@"Helvetica" size:13];
    lbl3.backgroundColor=[UIColor clearColor];
    [cell.contentView addSubview:lbl3];
    [lbl3 release];


    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

Upvotes: 5

Rushi
Rushi

Reputation: 4500

You can add 3 labels to every cell. And assign them value in UITableView. That's the only simple way of doing it.

You have to define the labels in your UITableViewCell declaration. Once it's done you can assign there values where you are declaring your UITableView.

So basically your each cell will contain the 3 labels which you can place anywhere you want.

Upvotes: 1

hchouhan02
hchouhan02

Reputation: 946

if you are using a UITableView then create a section for every data and make three rows in every section.

or

make a custom cell in which make three UILabels in each line.

Upvotes: 1

Related Questions