指定错误Cell的行的高度 - iOS

时间:2014-01-28 11:28:01

标签: ios uitableview

我的细胞高度有一个非常奇怪的问题。虽然在heightForRowAtIndexPath中,我在实际UItableView中为每个单元格的textView(如日志文本中所示)获得了正确的高度,但是高度被指定为错误的单元格。高度调整应用于第一个单元格(在顶部),而不是应用于最后一个单元格(底部)。日志文件是:

height = 73 
for cellText = inliulimkkhcelslktilkt vkr inuvilu i hkrhjknvhjkr hskn jhv ljhvsjkrhiuhiug vhriu hgiurhv kughkv s jhs hshkjh iuhguriu hgkj hgk hjkgh h
2014-01-28 13:12:03.312 anonymous[7838:70b] height = 18 
for cellText = cell add
2014-01-28 13:12:03.313 anonymous[7838:70b] height = 18 
for cellText = reload
2014-01-28 13:12:03.313 anonymous[7838:70b] height = 18 
for cellText = test again
2014-01-28 13:12:03.313 anonymous[7838:70b] height = 18 
for cellText = and another one
2014-01-28 13:12:03.313 anonymous[7838:70b] height = 18 
for cellText = lehsjgjksdk
2014-01-28 13:12:03.314 anonymous[7838:70b] height = 18 
for cellText = another one
2014-01-28 13:12:03.314 anonymous[7838:70b] height = 18 
for cellText = this is a reply test
2014-01-28 13:12:03.314 anonymous[7838:70b] height = 18 
for cellText = this is a reply test
2014-01-28 13:12:03.314 anonymous[7838:70b] height = 18 
for cellText = this is a reply test 1

但是在应用程序的实际运行中,单元格的实际位置正好相反(列表底部的大文本上升)意味着73.0f高度应用于“这是一个回复测试1 ”。有什么建议吗?

在cellForRow ..:

NSDictionary *dic = [[NSDictionary alloc] init];
dic = [passedArray objectAtIndex:indexPath.row];
message = [dic objectForKey:@"message"];
NSLog(@"message = %@",message);
username1 = [dic objectForKey:@"username"];
if ([username1 isEqualToString:username]) {
    cell1.textView.text = message;
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    UIImage *imageNotCropped = [UIImage imageWithData:[defaults objectForKey:@"profileImageData"]];
    NSLog(@"image: %@",imageNotCropped);
    cell1.imageView.image = imageNotCropped;
    cell1.imageView.contentMode = UIViewContentModeScaleToFill;
    cell1.imageView.layer.cornerRadius = 10;
    cell1.imageView.layer.masksToBounds = YES;
    cell1.textView.font = [UIFont fontWithName:@"HelveticaNeue-UltraLight" size:16];
}else{

在heightForRow中:

 NSString *cellText = [[passedArray objectAtIndex:passedArray.count-indexPath.row-1] objectForKey:@"message"];
 UIFont *cellFont = [UIFont fontWithName:@"HelveticaNeue-UltraLight" size:16.0];
 NSDictionary *attributes = @{NSFontAttributeName: cellFont};
CGRect rect = [cellText boundingRectWithSize:CGSizeMake(225, CGFLOAT_MAX)
                                               options:NSStringDrawingUsesLineFragmentOrigin
                                            attributes:attributes
                                               context:nil];
NSLog(@"height = %.f \nfor cellText = %@",rect.size.height, cellText);
return rect.size.height;

1 个答案:

答案 0 :(得分:0)

这可能是造成错误的原因:

 NSString *cellText = [[passedArray objectAtIndex:passedArray.count-indexPath.row-1] objectForKey:@"message"];

将其更改为:

 NSString *cellText = [[passedArray objectAtIndex:indexPath.row] objectForKey:@"message"];