【问题标题】:UILabel in UITableView in objective c目标c中UITableView中的UILabel
【发布时间】:2011-12-06 15:51:57
【问题描述】:

我想在我的表格视图中添加标签,我应该怎么做?

我想在 tableview 的每一行添加四个标签.. 谁能解释我如何为此编写代码。

【问题讨论】:

  • 这是一个问答网站,而不是“我可以危害密码?”地点。请更具体。
  • 对我来说似乎很具体。如果发布者确切地知道如何做他想做的事,他们可能就不需要问了。

标签: iphone objective-c ios uitableview uilabel


【解决方案1】:

实现以下方法,

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

【讨论】:

    【解决方案2】:

    UITableViewCell 对象具有contentView 属性。将任何自定义视图添加为 contentView 的子视图。

    【讨论】:

      【解决方案3】:

      您想要的是“自定义 UITableViewCell”。如果你用谷歌搜索,你会发现很多教程和信息。

      例如,这里有一个教程:http://www.galloway.me.uk/tutorials/custom-uitableviewcell/

      【讨论】:

        【解决方案4】:

        您可以使用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/

        【讨论】:

          【解决方案5】:

          您的 tableview 行是一个视图,您可以简单地添加一些标签作为子视图,尽管我不知道这是否是最好的做法。

          你想完成什么? 4 个标签对于 iPhone 的小屏幕来说似乎有点多。

          【讨论】:

            猜你喜欢
            • 2012-01-19
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-09-27
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多