簡體   English   中英

目標c中的UITableView中的UILabel

[英]UILabel in UITableView in objective c

我想在表視圖中添加標簽,該怎么辦?

我想在tableview的每一行中添加四個標簽。.有人可以解釋一下如何為此編寫代碼。

UITableViewCell對象具有contentView屬性。 將任何自定義視圖添加為contentView子視圖。

您想要的是一個“自定義UITableViewCell”。 如果您使用Google進行搜索,則會發現很多教程和信息。

例如,這是一個教程: http : //www.galloway.me.uk/tutorials/custom-uitableviewcell/

實施以下方法,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [self getCellContentView:CellIdentifier];
    }

    UILabel *lblManuName1 = (UILabel *)[cell viewWithTag:1];
    UILabel *lblManuName2 = (UILabel *)[cell viewWithTag:2];

    UILabel *lblCmpnyName1 = (UILabel *)[cell viewWithTag:3];
    UILabel *lblCmpnyName2  = (UILabel *)[cell viewWithTag:4];


        lblManuName1.text = @"Lbl 1" ; 
    lblManuName2.text = @"Lbl 2" ;
    lblManuName2.text = @"Lbl 3" ; 
    lblCmpnyName2.text = @"Lbl 4" ;

    return cell;
}

- (UITableViewCell *) getCellContentView:(NSString *)cellIdentifier {

    CGRect CellFrame = CGRectMake(0, 0, 250, 30);

    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];

    CGRect LabelManuName1Frame;
    CGRect LabelManuName2Frame ;

    CGRect LabelCmpnyName1Frame;
    CGRect LabelCmpnyName2Frame;

    LabelManuName1Frame = CGRectMake(10, 5, 75, 15);
    LabelManuName2Frame = CGRectMake(59, 5, 205, 15);

    LabelCmpnyName1Frame = CGRectMake(10, 22, 100, 15);
    LabelCmpnyName2Frame = CGRectMake(59, 22, 205, 15);



    if (orientation == UIInterfaceOrientationLandscapeLeft || 
        orientation == UIInterfaceOrientationLandscapeRight) {

        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
            //LabelManuName1Frame = CGRectMake(10, 13.5, 180, 15);
            //LabelManuName2Frame = CGRectMake(59, 13.5, 580, 15);
            LabelManuName1Frame = CGRectMake(10, 5, 70, 30);
            LabelManuName2Frame = CGRectMake(80, 5, 350, 30);

            //LabelCmpnyName1Frame = CGRectMake(410, 13.5, 235, 15);
            //LabelCmpnyName2Frame = CGRectMake(459, 13.5, 580, 15);
            LabelCmpnyName1Frame = CGRectMake(435, 5, 70, 30);
            LabelCmpnyName2Frame = CGRectMake(505, 5, 350, 30);

        }else{
            LabelManuName1Frame = CGRectMake(10, 5, 75, 15);
            LabelManuName2Frame = CGRectMake(59, 5, 370, 15);

            LabelCmpnyName1Frame = CGRectMake(10, 22, 130, 15);
            LabelCmpnyName2Frame = CGRectMake(59, 22, 370, 15);
        }

    } else {
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
            //LabelManuName1Frame = CGRectMake(10, 13.5, 180, 15);
            //LabelManuName2Frame = CGRectMake(59, 13.5, 300, 15);
            LabelManuName1Frame = CGRectMake(10, 5, 70, 30);
            LabelManuName2Frame = CGRectMake(80, 5, 250, 30);

            //LabelCmpnyName1Frame = CGRectMake(370, 13.5, 205, 15);
            //LabelCmpnyName2Frame = CGRectMake(419, 13.5, 200, 15);
            LabelCmpnyName1Frame = CGRectMake(330, 5, 70, 30);
            LabelCmpnyName2Frame = CGRectMake(400, 5, 250, 30);
        }else { 
            LabelManuName1Frame = CGRectMake(10, 5, 75, 15);
            LabelManuName2Frame = CGRectMake(59, 5, 205, 15);

            LabelCmpnyName1Frame = CGRectMake(10, 22, 100, 15);
            //Increse width of LabelCmpnyName2Frame
            LabelCmpnyName2Frame = CGRectMake(59, 22, 205, 15);
        }
    }



    UILabel *lblTemp;

    UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CellFrame reuseIdentifier:cellIdentifier] autorelease];

    //Initialize Label with tag 3.
    lblTemp = [[UILabel alloc] initWithFrame:LabelManuName1Frame];
    lblTemp.tag = 1;
    //lblTemp.font =  [UIFont fontNamesForFamilyName:@"Arial"];
    lblTemp.font =  [UIFont fontWithName:@"Arial-BoldMT" size:13];
    lblTemp.backgroundColor = [UIColor clearColor];
    [cell.contentView addSubview:lblTemp];
    [lblTemp release];  

    //Initialize Label with tag 4.
    lblTemp = [[UILabel alloc] initWithFrame:LabelManuName2Frame];
    lblTemp.tag = 2;
    //lblTemp.font =  [UIFont fontNamesForFamilyName:@"Arial"];
    lblTemp.font =  [UIFont fontWithName:@"Arial-BoldMT" size:13];
    lblTemp.backgroundColor = [UIColor clearColor];
    [cell.contentView addSubview:lblTemp];
    [lblTemp release];  

    //Initialize Label with tag 5.
    lblTemp = [[UILabel alloc] initWithFrame:LabelCmpnyName1Frame];
    lblTemp.tag = 3;
    //lblTemp.font =  [UIFont fontNamesForFamilyName:@"Arial"];
    lblTemp.font =  [UIFont fontWithName:@"Arial-BoldMT" size:13];
    lblTemp.backgroundColor = [UIColor clearColor];
    [cell.contentView addSubview:lblTemp];
    [lblTemp release];  

    //Initialize Label with tag 6.
    lblTemp = [[UILabel alloc] initWithFrame:LabelCmpnyName2Frame];
    lblTemp.tag = 4;
    //lblTemp.font =  [UIFont fontNamesForFamilyName:@"Arial"];
    lblTemp.font =  [UIFont fontWithName:@"Arial-BoldMT" size:13];
    lblTemp.backgroundColor = [UIColor clearColor];
    [cell.contentView addSubview:lblTemp];
    [lblTemp release];

    return cell;
}

您可以使用UITableViewCell輕松地完成此操作。 Mmake一個自定義單元添加所需數量的標簽。 在表格視圖中添加該自定義單元格。 請參閱以下代碼:

@interface EditingTableCell : UITableViewCell 
{  
    UILabel       *status;
    UILabel       *valueofplayername;
}

@property (nonatomic,retain) IBOutlet UILabel *status;
@property (nonatomic,retain) IBOutlet UILabel *valueofplayername;

從IB獲取一個uitableviewcell ,添加標簽並與IBOutlet鏈接。 之后,在您的cellForRowAtIndexPath委托中添加以下代碼:

EditingTableCell *cell = (EditingTableCell *)[tableView dequeueReusableCellWithIdentifier:IngredientsCellIdentifier];
if (cell == nil)
{
    [[NSBundle mainBundle] loadNibNamed:@"EditingTableCell" owner:self options:nil];
    cell = edittableviewcell;
    self.edittableviewcell = nil;
}

檢查此鏈接: http : //www.icodeblog.com/2009/05/24/custom-uitableviewcell-using-interface-builder/

您的tableview行是一個視圖,您可以簡單地添加一些標簽作為子視圖,盡管我不知道這是否是最好的選擇。

你想達到什么目的? 對於iPhone的小屏幕來說,“ 4個標簽”似乎有點過多。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM