【问题标题】:How to align the Label in UITableview?如何在 UITableview 中对齐标签?
【发布时间】:2013-02-09 00:30:10
【问题描述】:

我有一个UITableView,其中有不同大小的图像。所以当我运行程序时,我在 TableView 中得到了不同的标签起点。

我希望所有标签都从同一点开始。

对齐对我不起作用。我希望所有标签都从左侧一定距离开始。

有什么解决办法吗?

【问题讨论】:

  • 你在使用 TableView 的自定义单元格吗?如果不使用 customcell
  • TableView的customcell请看教程
  • 好的,谢谢,我认为自定义调用有效..非常感谢..
  • 点赞我的评论,人们就会知道答案

标签: ios ios5 ios6 ios4 ios-simulator


【解决方案1】:

从 UITableViewCell 派生一个类。

重写方法layoutSubviews。在此方法中,为所有组件(如 imageview、主标签等)设置适当的框架。

CustomCell.h

@interface CustomCell : UITableViewCell
{

}

@end

CustomCell.m

@implementation CustomCell

    -(void) layoutSubviews
    {
         [super layoutSubviews];
        self.imageView.frame = SOME_FRAME;
        self.textLabel.frame = SOME_OTHER_FRAME;

    }

@end

现在不要从 cellForRowAtIndexPath 中的 UITableViewCell 创建单元格,而是使用上面的单元格。

【讨论】:

  • 嘿朋友们,这个链接帮我解决了我的问题.. appcoda.com/customize-table-view-cells-for-uitableview>你也可以试试
【解决方案2】:

你的标签位置因为图像而受到干扰?

如果是,那么有两种方法可以做到这一点 1

[imgView setContentMode:UIViewContentModeScaleToFill]

2 你应该首先获取图像视图的宽度,然后添加一些空间因子,然后为你的 uilabel 设置适当的起点。

UILabel *lbl = ...;
UIImageView * imgView = ....;
CGRect lblFrame = lbl.frame;
lblFrame.origin.x = imgView.frame.origin.x + imgView.frame.size.width + 10;
[lbl setFrame:lblFrame];

编辑:

如果调整 uiimage 的大小不是问题,那么调整你的图像大小...

+ (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
    //UIGraphicsBeginImageContext(newSize);
    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();    
    UIGraphicsEndImageContext();
    return newImage; }

块引用

> [CLASSNAME imageWithImage:yourImage scaledToSize:CGSizeMake(50,50)];

【讨论】:

    【解决方案3】:

    试试这段代码,它对对齐 UiTableView 中的 UILabel 非常有用: UILabel *label=[[UILabel alloc]init];

        label.frame=CGRectMake(80, 2, 180, 60);
    
        label.backgroundColor=[UIColor clearColor];
    
        label.text=[ShopListOfItems objectAtIndex:indexPath.row];
    
        label.textColor=[UIColor whiteColor];
    
        label.font=[UIFont boldSystemFontOfSize:20];
    
        label.numberOfLines=2;
    
        [cell.contentView addSubview:label];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-08
      • 1970-01-01
      • 1970-01-01
      • 2019-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多