Ios UITableView自定义标签集框架不工作
我有一个时间标签下面的动态标签-评论标签,我想放置 注释标签下的时间标签。但是,当注释标签打开时,时间标签将隐藏 更大。看起来时间标签的Y偏移量没有改变Ios UITableView自定义标签集框架不工作,ios,Ios,我有一个时间标签下面的动态标签-评论标签,我想放置 注释标签下的时间标签。但是,当注释标签打开时,时间标签将隐藏 更大。看起来时间标签的Y偏移量没有改变 static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; spComments *comment=[_data objectAtIndex:in
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
spComments *comment=[_data objectAtIndex:indexPath.row];
UILabel *timeLabel;
UILabel *commentLabel;
//do not recreate controls, just change the contents each time
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
double likeHeight=40;
double likeWidth=40;
commentLabel=[[UILabel alloc]initWithFrame:CGRectMake(5.0,10.0,
cell.contentView.frame.size.width-70, cell.contentView.frame.size.height)];
[commentLabel setFont:self.font];
[cell.contentView addSubview:commentLabel];
[commentLabel setTag:1]; //have to set it after added
//like button
UIButton *likeButton = [UIButton buttonWithType:UIButtonTypeCustom];
[likeButton setFrame:CGRectMake(cell.contentView.frame.size.width-40, 10, likeWidth, likeHeight)];
//[likeButton setImage:[UIImage imageNamed:@"heart_icon_small"] forState: UIControlStateNormal];
[likeButton setBackgroundImage:[UIImage imageNamed:@"heart_icon_small"] forState:UIControlStateNormal];
// likeButton.imageEdgeInsets = UIEdgeInsetsMake(-50, 0, 0, 0);
[cell.contentView addSubview:likeButton];
timeLabel=[[UILabel alloc]init];
UIFont *timeFont=[UIFont fontWithName:@"AppleSDGothicNeo-Light" size:10];
[timeLabel setFont:timeFont];
[cell.contentView addSubview:timeLabel];
[timeLabel setTag:2];
}
commentLabel = (UILabel*)[cell.contentView viewWithTag:1];
[commentLabel setText:comment.message];
[commentLabel adjustLabel];
NSLog(@"Comment height %f",commentLabel.frame.size.height);
[commentLabel setBackgroundColor:[UIColor redColor]];
timeLabel = (UILabel*)[cell.contentView viewWithTag:2];
[timeLabel setFrame:CGRectMake(5.0, commentLabel.frame.size.height+10, 100, 50)];
[timeLabel setText:comment.sincewhen];
[timeLabel setBackgroundColor:[UIColor blueColor]];
return cell;
你的问题是这行:
[timeLabel setFrame:CGRectMake(5.0, commentLabel.frame.size.height+10, 100, 50)];
应改为:
[timeLabel setFrame:CGRectMake(5.0, commentLabel.frame.size.height + commentLabel.frame.origin.y + 10, 100, 50)];
我想,这会让你的生活更轻松。试试:
cell.clipsToBounds = NO;
可能时间标签已经移出了cellContent。如果您的行高为100;所以
commentLabel=[[UILabel alloc]initWithFrame:CGRectMake(5.0,10.0,cell.contentView.frame.size.width-70,50)];
timeLabel必须这样做(不能使用contentView.frame.size.height):
timeLabel.frame=CGRectMake(5.0,60.0+10,cell.contentView.frame.size.width-70,30)];
为了澄清,这是cellForRowAtIndexPath方法,对吗?您的UILabel的高度是否确实发生了变化?在这一行中。。commentLabel=[[UILabel alloc]initWithFrame:CGRectMake(5.0,10.0,cell.contentView.frame.size.width-70,cell.contentView.frame.size.height)];为什么y轴为10.0,高度为cell.contentView.frame.size.height???原点始终保持在10。