我有一个似乎无法解决的特别问题我已经查看了我的代码几个小时,但似乎无法解决问题。
当我将消息添加到我的表格视图时,消息会溢出 UITableViewCell 中的包装器,有时包装器会被单元格截断。
我如何确保我的包装器总是足够大以容纳 txt (lblMessage) 并且单元格足够大以容纳包装器。
提前致谢 !
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat height = [self heightForMessage:indexPath];
if(height < 50)
{
NSLog(@"Normal : %d , row : %d", 100,indexPath.row);
return 100;
}
else
{
NSLog(@"Custom : %f , row : %d", 70 + height,indexPath.row);
return 70 + height;
}
}
-(CGFloat)heightForMessage:(NSIndexPath *)indexPath
{
MessageObject * model = [self.chats objectAtIndex:indexPath.row];
CGSize size = [model.message sizeWithFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:12] constrainedToSize:CGSizeMake(290, 100000) lineBreakMode:NSLineBreakByWordWrapping];
return size.height;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * NewsCellIdentifer = @"NewsCellIdentifier";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NewsCellIdentifer];
UILabel * lblContact;
UILabel * lblMessage;
UILabel * lblDate;
UIView * wrapper;
if (cell == nil)
{
/* Top menu for login */
wrapper = [[UIView alloc] initWithFrame:CGRectMake(10, 5, 300, 90)];
[wrapper setBackgroundColor:[self.delegate.color colorWithAlphaComponent:0.6f]];
[wrapper.layer setCornerRadius:6.0f];
[wrapper.layer setShadowOffset:CGSizeMake(0, 2)];
[wrapper.layer setShadowRadius:2.0f];
[wrapper.layer setShadowOpacity:0.5f];
[wrapper viewWithTag:19];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:NewsCellIdentifer];
/* Contact Name */
lblContact = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 150, 30)];
[lblContact setFont:[UIFont fontWithName:@"HelveticaNeue-Medium" size:13]];
[lblContact setTag:13];
[lblContact setTextColor:[self.delegate colorFromHexString:@"#fffbff"]];
[lblContact setBackgroundColor:[UIColor clearColor]];
/* Received Message */
lblMessage = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, 280, 50)];
[lblMessage setNumberOfLines:0];
[lblMessage setLineBreakMode:NSLineBreakByWordWrapping];
[lblMessage setBackgroundColor:[UIColor clearColor]];
[lblMessage setFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:12]];
[lblMessage setTextColor:[self.delegate colorFromHexString:@"#fffbff"]];
[lblMessage setTag:14];
/* Date received */
lblDate = [[UILabel alloc] initWithFrame:CGRectMake(10, 65, 65, 30)];
[lblDate setText:@"4 hours ago"];
[lblDate setFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:11]];
[lblDate setTextColor:[self.delegate colorFromHexString:@"#fffbff"]];
[lblDate setTag:15];
/* Subview Logic */
[wrapper addSubview:lblContact];
[wrapper addSubview:lblMessage];
[wrapper addSubview:lblDate];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell.contentView addSubview:wrapper];
}
else
{
lblContact = (UILabel *)[cell.contentView viewWithTag:13];
lblMessage = (UILabel *)[cell.contentView viewWithTag:14];
wrapper = [cell.contentView viewWithTag:19];
}
MessageObject * model = [self.chats objectAtIndex:indexPath.row];
CGFloat height = [self heightForMessage:indexPath];
[lblMessage setFrame:CGRectMake(10, 25, 280, height)];
[lblMessage setText:model.message];
[lblContact setText:model.clientModel.firstName];
if(height < 50)
{
NSLog(@"wrapper size : %d for row %d ", 90, indexPath.row);
[wrapper setFrame:CGRectMake(10, 5, 300, 90)];
}
else
{
NSLog(@"wrapper size : %f for row %d ", height + 60, indexPath.row);
[wrapper setFrame:CGRectMake(10, 5, 300, height + 60)];
[lblDate setFrame:CGRectMake(10, height + 20, 65, 30)];
}
[cell.contentView setBackgroundColor:[UIColor greenColor]];
[cell setBackgroundColor:[UIColor clearColor]];
return cell;
}