将`UITableViewCell`分割成部分。

4

我正在创建一个应用程序,需要将一个单元格分成三个部分。例如,我需要在一个单元格中给出标题,并立即在下方显示相应的数据。

我该如何实现?


- (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

在主屏幕上,我创建了一个主余额文本框和两个按钮,如收入、支出,还有一个最终显示按钮。当我点击收入时,它会显示另一个视图,在该视图中添加收入文本框和一个添加到主余额的按钮。我在文本框中输入收入金额,然后点击保存,它将保存在数据库中。支出也是同样的操作,然后我点击添加到主余额按钮,它将在主余额文本框中加减相应的金额,我在主屏幕上显示。然后在主屏幕上点击最终显示按钮,它将在表格视图中显示三个标签:收入、支出、主余额。我获取收入和支出的值,但它只显示我在设计时查询数据库所输入的初始值。

我的问题:假设我添加了4条数据,最终显示将显示所有这些数据,即使我添加更多的数据,它也会在最终显示中显示...我希望您能理解我的意思,请指导我,我很困惑。

4个回答

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;
}

现在我有三个标题,如收入、支出和可用余额,我想从不同的数据库中获取这些数据,而这些数据库我已经提取了。 - Hardik rami
不,我只是在 cellForRowAtIndex 中编写代码。UILabel *temp2 = [[UILabel alloc] initWithFrame:CGRectMake(109, 0, 106, 44)]; temp2.text = @"temp2"; temp2.backgroundColor = [UIColor clearColor]; [cell addSubview:temp2] 并检查 rowIndex。 - Hardik rami
我做了三次相同的操作并获取到详细信息,但是我只能获得最初的详细信息,也就是说当我添加或删除收入/支出后,当我查看列表时它仍然只显示最初的详细信息,而不显示我最近添加或删除的详细信息。 - Hardik rami
我只是在这一行进行更改,类似于 database *objdata =[mydelegate.myarray objectAtIndex:indexPath.row-1]; - Hardik rami
嘿兄弟,我知道那个人,我需要帮助。 - Hardik rami
显示剩余4条评论

1
如果你正在使用一个 UITableView,那么为每个数据创建一个 section,并在每个 section 中创建三行。
或者
创建一个自定义的单元格,在每一行中创建三个 UILabel。

UILabel *temp2 = [[UILabel alloc] initWithFrame:CGRectMake(109, 0, 106, 44)]; temp2.text = @"temp2"; temp2.backgroundColor = [UIColor clearColor]; [cell addSubview:temp2] - Hardik rami

1

你可以在每个单元格中添加3个标签,并在UITableView中为它们分配值。这是唯一简单的方法。

你必须在UITableViewCell声明中定义标签。一旦完成,你就可以在声明UITableView时为它们分配值。

所以基本上,每个单元格都将包含3个标签,你可以将它们放在任何位置。


-2

你可以添加三个UIViews来完成


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接