Iphone UITableView单元格中的UILabel

Iphone UITableView单元格中的UILabel,iphone,ipad,uitableview,uilabel,Iphone,Ipad,Uitableview,Uilabel,我是iPhone开发者的新手 我使用的是UITableView,我希望UITableView的单元格中总共有6个标签,L.H.S上有3个标签,R.H.S上有3个标签 这是我的代码片段 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 180; } - (UITableViewCell *)tableView:(UITableView

我是iPhone开发者的新手

我使用的是
UITableView
,我希望
UITableView的
单元格中总共有6个标签,L.H.S上有3个标签,R.H.S上有3个标签

这是我的代码片段

-(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;
}
但是我无法正确地看到我的UILabel

任何帮助都将不胜感激。


编辑:

要执行此类工作,我建议您创建一个自定义单元格。(添加新文件->目标C类->UItableViewCell)

使用属性定义6个标签

#import <UIKit/UIKit.h>

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

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

定义6个标签并为此设置框架。
然后,将子视图添加到tableViewCell contentView中。

请参阅更正的代码:

- (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;
}
你犯了太多错误:

LHS1是可见的,但您可以用LHS3文本覆盖它的文本:
AlertNameLHS.text=@“到期日:”

LHS2不可见,因为您仅对其进行初始化并将其添加为子视图,而不是配置了LHS1

LHS3不可见,因为单元格高度为180,其y坐标为190,您也不设置它的文本,而是设置LHS1文本


RHS标签框不正确且超出单元格框,您应该使用frameR,但使用frameL,我还建议您在每一步向y坐标添加60个像素。

是否要在TableView中创建6个单元格搜索创建自定义UITableViewCell@Vineessh:不是6单元,每个单元格上有6个标签。代码中可能有多个问题?你能用截图解释一下预期结果和当前结果吗?看我的截图,当我滚动标签时,标签被隐藏了。这是一个很好的方法,仍然没有答案为什么有问题的代码没有显示标签。哦,这不适合评论区,我必须自己回答(我不是OP,呵呵)很酷,但回答这个问题没用,不是吗?
- (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;
}