2012-07-12 87 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