UICustom 单元格在 iOS 7 中不显示标签

Posted

技术标签:

【中文标题】UICustom 单元格在 iOS 7 中不显示标签【英文标题】:UICustom cell not showing labels in iOS 7 【发布时间】:2014-09-12 06:00:02 【问题描述】:

我添加了带有标签的自定义UItableViewCell。标签在 ios 6 中可见,但在 iOS 7 中不可见。

-(void)layoutSubviews

   [super layoutSubviews];
   self.textLabel.frame = CGRectMake(90.0f ,-5.0f, 200.0f, 50.0f);
   self.imageView.frame = CGRectMake(3.0f , 2.0f, 75.0f, 55.0f);




-(void)drawRect:(CGRect)rect

   self.imageView.layer.cornerRadius = 5;
   self.imageView.layer.masksToBounds = YES;
   self.imageView.layer.borderColor = [UIColor colorWithRed:0.0 green:0.4 blue:0.7 alpha:0.9].CGColor;
   self.imageView.layer.borderWidth = 2;


   [self.textLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:12]];
   self.textLabel.backgroundColor = [UIColor clearColor];

   [[UIColor grayColor] set];
   [videoDurationTextLabel  drawInRect:CGRectMake(90, 30, 190, 10) withFont:[UIFont fontWithName:@"Helvetica" size:12]];

    [videoFileSizeLabel drawInRect:CGRectMake(170, 30, 190, 15) withFont:[UIFont fontWithName:@"Helvetica" size:12]];



TableViewController 类

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

    static NSString *CellIdentifier = @"LazyTableCell";

    VideoCustomCell *cell = [self.tblView dequeueReusableCellWithIdentifier:CellIdentifier];


if (cell == nil)

    cell = [[[VideoCustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                   reuseIdentifier:CellIdentifier] autorelease];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;



cell.textLabel.text = videoRecord.videoTitle;
cell.videoFileSizeLabel = [NSString stringWithFormat:@"%.4lfMb",totalSpaceInMB];
[cell.imageView setImage:[UIImage imageWithData: imgData]];
cell.videoDurationTextLabel = videoRecord.duration;

【问题讨论】:

你试过在init方法中设置标签的框架吗? 查看此链接apeth.com/iOSBook/ch21.html#_custom_cells 【参考方案1】:

试试这个代码。

- (void) layoutSubviews 
[super layoutSubviews];
CGRect cvb = self.contentView.bounds;
CGRect imf = self.imageView.frame;
imf.origin.x = cvb.size.width - imf.size.width;
self.imageView.frame = imf;
CGRect tf = self.textLabel.frame;
tf.origin.x = 5;
self.textLabel.frame = tf;
 

- (void) viewDidLoad 

[super viewDidLoad];

UINib *cellNib = [UINib nibWithNibName:@"CustomTableCell" bundle:[NSBundle mainBundle]];
[self.tableView registerNib:cellNib forCellReuseIdentifier:@"CustomTableCell"];

 

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

static NSString *CellIdentifier = @"CustomTableCell";
CustomTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if(cell == nil)

    cell = [[CustomTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];


cell.titleLabel.text ="test text";
[cell.imageView setImage:[UIImage imageWithData: imgData]];


return cell;
 

【讨论】:

【参考方案2】:

您必须使用您的单元格标识符名称来命名您的自定义单元格。

正如我在您的UITableViewCell 代码中看到的,您的单元标识符名称是LazyTableCell。将您的单元格重命名为相同的名称。

【讨论】:

以上是关于UICustom 单元格在 iOS 7 中不显示标签的主要内容,如果未能解决你的问题,请参考以下文章

带有公式的单元格在 Excel 2010 中不计算

ios 7 uitableview 所选单元格在 beginupdates 后失去边框

向左滑动删除单元格在 ios 8 中无法正常工作,但在 ios 7 中有效

collectionView 单元格在 tapGestureRecognizer 函数中不起作用

iOS - 使 UITableView 与自定义单元格在按钮单击时可编辑

excel中怎样用vba使单元格在特定条件下才可以编辑?