동적 인 TextView와 수직으로 가운데 정렬해야하는 다른 객체를 가진 UITableViewCell이 있습니다.UITableview, 동적 높이가있는 셀의 개체를 세로로 가운데에 맞 춥니 다?
지금 나는 UITextView의 높이를 계산하고 모든 것을 제자리에 설정하기 위해 참조로 사용합니다.
내가 가진 문제는 때로는 잘못 계산되고 항상 중간에 있어야하는 "priceLabel"이 아래쪽에 있다는 것입니다. 세포가 다시 눈에 띄게 될 수 있도록 스크롤하는 시간의 20 %가 발생합니다.
세포의 높이를 얻는 방법이 있습니까? 왜 잘못 계산합니까? 또한이 문제가 나타날 때 UITextView를 절반으로 줄입니까?
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
CGSize size;
NSString *text;
/* Height of each cell */
text = @"Some different lengths of text";
size = [text sizeWithFont:[UIFont fontWithName:@"TrebuchetMS" size:17] constrainedToSize:CGSizeMake(theTableView.bounds.size.width - 260, 800) lineBreakMode:UILineBreakModeWordWrap];
float totalSize = size.height + 10;
if (totalSize < 120) {
return 120;
} else {
return totalSize;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
CGSize size;
NSString *text;
float totalSize;
/* Height of each cell */
text = @"Some different lengths of text";
size = [text sizeWithFont:[UIFont fontWithName:@"TrebuchetMS" size:17] constrainedToSize:CGSizeMake(theTableView.bounds.size.width - 260, 800) lineBreakMode:UILineBreakModeWordWrap];
if (size.height + 10 < 120) {
totalSize = 120;
} else {
totalSize = size.height + 10;
}
}
/* Createing a new Cell */
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
/* Create the text Label */
textView = [[[UITextView alloc] initWithFrame: CGRectMake(90,35,theTableView.bounds.size.width - 260 , totalSize)] autorelease];
[cell.contentView addSubview:textView];
textView.tag = TEXT_LABEL_TAG;
textView.backgroundColor = [UIColor clearColor];
textView.textColor = [UIColor blackColor];
textView.font = [UIFont fontWithName:@"TrebuchetMS" size:17];
textView.editable = NO;
textView.userInteractionEnabled = NO;
/* Create price Label */
priceLabel = [[[UILabel alloc] initWithFrame: CGRectMake(theTableView.bounds.size.width - 225, (totalSize/2) - (35/2), 120 , 35)] autorelease];
[cell.contentView addSubview:priceLabel];
priceLabel.tag = PRICE_LABEL_TAG;
priceLabel.textAlignment = UITextAlignmentRight;
priceLabel.backgroundColor = [UIColor clearColor];
priceLabel.textColor = [UIColor redColor];
priceLabel.font = [UIFont fontWithName:@"TrebuchetMS-Bold" size:22];