2012-02-09 205 views
4

我正在创建一个应用程序,我必须将一个单元分成三部分。例如,我必须在一个单元格中给出标题,并在下面显示相应的数据。将`UITableViewCell`分成几部分

我该怎么做?


- (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 [email protected]"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 [email protected]"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 [email protected]"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 

在主屏幕上创建一个主Blaance文本框和两个按钮一样的收入,费用和最后一个显示按钮。当我点击收入时,它会显示另一个视图,其中添加收入的文本框和一个按钮添加到主balance.I键入收入金额在该文本框中,然后单击保存,它将保存在数据库中,我做同样的费用,然后点击添加到主余额按钮,它将被添加或减去主屏幕上显示的主余额文本框中的金额。然后单击主屏幕上的最终显示按钮,它将显示三个标签收入,费用,主平衡..在TableView.I获得收入和费用下的价值但它仅显示我在数据库中输入的初始值在设计时按查询。

我的问题: -想我加4个数据,那么最终显示将显示,即使我添加更多的数据是在最终显示器还显示所有数据....我希望你能得到我的point..please引导我我很困惑。

回答

1

您可以为每个单元添加3个标签。并在UITableView中赋值。这是做这件事的唯一简单方法。

您必须在您的UITableViewCell声明中定义标签。一旦完成,你可以在那里指定你正在声明你的UITableView的值。

所以基本上你的每个单元格将包含3个标签,你可以放在任何你想要的地方。

1

如果您使用的是UITableView,则为每个数据创建一个节并在每个节中创建三行。

使自定义电池,其中使3 UILabels中的每一行。

+0

u能张贴代码,请。 – 2012-02-09 12:12:41

+0

UILabel * temp2 = [[UILabel alloc] initWithFrame:CGRectMake(109,0,106,44)]; temp2.​​text = @“temp2”; temp2.​​backgroundColor = [UIColor clearColor]; [cell addSubview:temp2] – 2012-02-09 12:16:36

+0

: - 我喜欢上面的代码 – 2012-02-09 12:17:10

5

使用此代码它会帮助你。

- (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; 
} 
+0

谢谢你..我做了它。像现在的单元格.. – 2012-02-09 13:06:28

+0

现在我得到像收入费用和avilable bal三个标题现在我想要获得的数据从不同的数据库,我已经取得 – 2012-02-09 13:08:40

+0

'NSMutableArray'中获取的数据? – hchouhan02 2012-02-09 13:10:28