2012-07-12 37 views
-1

我是新来的iPhone开发者的细胞,的UILabel中的UITableView

我使用UITableView,我想总6个标签在我UITableView 3标签上LHS和RHS

这里3标签的Cell是我代码片段,

-(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]; 
     [email protected]"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]; 
     [email protected]"Alert Month :"; 
     [cell.contentView addSubview:AlertMonthLHS]; 

     frameL.origin.y += 120; 
     UILabel *DueOnLHS = [[UILabel alloc] initWithFrame:frameL]; 
     DueOnLHS.font=[UIFont systemFontOfSize:16.0]; 
     [email protected]"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。

任何帮助将不胜感激。

编辑:1

+0

你的意思是在的tableView – 2012-07-12 08:48:06

+0

创建6芯不要你看到了哪些标签? – 2012-07-12 08:49:02

+0

搜索创建自定义UITableViewCell – 2012-07-12 08:53:46

回答

1

见更正后的代码:

- (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]; 
     [email protected]"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]; 
     [email protected]"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]; 
     [email protected]"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 [email protected]"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文字:[email protected]"Due On :";

的LHS2 ISN “T可见的,因为你只初始化,并添加作为一个子视图,配置LHS1,而不是它

的LHS3是不是因为C可见ell的高度是180,y的坐标是190,你也不用设置它的文本来设置LHS1文本。

的RHS标签帧是不正确的进出细胞框架的,你应该使用成帧器,但使用frameL,我也建议你要添加60个像素为y的每一步协调。

1

要进行这方面的工作,我建议你创建一个定制的细胞。 (添加新文件 - > Objective-C类 - >的UITableViewCell)

定义财产6个标贴

#import <UIKit/UIKit.h> 

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

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

现在创建一个新的空视图(仅笔尖文件)命名Democell(为方便起见) 现在,从nib文件中删除视图,然后从库中拖放一个Table View Cell,并将它放在视图的nib文件中。

现在选择您的tableviewcell,并在其身份检查,改变它的类DemoCell 所以这会将你的笔尖文件,自定义单元格类..

在笔尖文件

现在,拖&下降6标签,根据您的要求定位它们,并在文件所有者链接所有lbl与lblOne,lblTwo ,,,, ....

现在您的自定义单元格已完成。 要使用此单元格在表中的的cellForRowAtIndexPath

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *cellIdentifier = @"cell"; 
    DemoCell *cell= (DemoCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
    if (cell ==nil) 
    { 
     cell=[[[NSBundle mainBundle]loadNibNamed:@"DemoCell" owner:self options:nil] lastObject]; 
    } 


cell.lblone.textlabel.text = @"lblOne"; 
+0

这是一个非常好的方法,仍然没有答案,为什么有问题的代码不显示标签。 – 2012-07-12 09:38:01

+0

你能告诉我你到现在为止尝试过什么吗? – iCoder 2012-07-12 09:46:12

+0

哦,那不适合评论区域,我不得不回答自己(我不是OP,嘿)http://stackoverflow.com/a/11449679/792677 – 2012-07-12 10:15:14

-1

定义6个标签和设置该帧。 然后,将addSubView添加到tableViewCell contentView。

+0

你认为它有什么作用? – 2012-07-12 09:36:11

+0

很酷,但对这个问题的答案没用,不是吗? – 2012-07-12 09:38:58

相关问题