iphone - 将 `UITableViewCell`分成几部分

标签 iphone ios uitableview

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

我该怎么做?


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

在主屏幕上,我创建了一个 Main Blaance 文本框和两个按钮,如收入、支出和一个最终显示按钮。当我单击收入时,它会显示另一个 View ,其中在文本框中添加收入,并在其中添加一个按钮以添加到主余额。我在该文本框中键入收入金额,然后单击保存,它将保存在数据库中,我对费用执行相同操作,然后单击添加到主余额按钮,它将添加或减去我在主屏幕上显示的主余额文本框中的金额。然后单击主屏幕上的最终显示按钮,它将在 TableView 中显示三个标签收入、支出、主余额。我获取收入和费用下的值但它仅显示我在设计时通过查询输入数据库的初始值。

我的问题:- 假设我添加了 4 个数据,那么最终显示将显示所有这些数据,即使我添加更多数据它也会显示在最终显示中....我希望你能得到我的点..请指导我,我很困惑。

最佳答案

使用此代码,它会对您有所帮助。

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

关于iphone - 将 `UITableViewCell`分成几部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9210455/

相关文章:

iphone - 常规/组 UITableViewCell 中的字符数

iphone - 根据自定义单元格高度动态 uitableviewcell 高度

ios - 如何在 iOS 中使 JSON 文件可本地化?

ios - 向导航栏添加约束

ios - Swift:当 UITableView 滚动太快时应用程序崩溃

iphone - 隐藏特定 TableView 部分的 TableViewHeaderView

iphone - 使用 afnetworking 在 json 有效负载中嵌入 base64 的正确方法

ios - 在 Swift 中识别 iPhone UID 的最佳方法是什么?

iOS Metal : How to Capture GPU Frame properly?

ios - 如何使用TableView“选择行位于”方法更新CollectionView