我有一些UITableViewCell,TextView是动态的,还有一些其他对象应该是垂直居中的 .
目前我计算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];
2 回答
当我在initWithFrame:调用中同时定义子视图的帧框架及其位置时,我遇到了一些问题 . 看起来帧在变换期间不是很稳健 . 初始化子视图后,我有更好的运气设置边界和中心属性 . 这也可以让您更好地控制定心 .
您的代码看起来很好,除了坐标中有整数数学 . 所以,试着用
它可以帮助,但我不确定 .