コンテンツのサイズ/長さに基づいてセルの高さのサイズを変更する必要があります。
10214 次
5 に答える
4
UITableViewCell
高さを動的に変更するには、このチュートリアルを参照してください。
また、このチュートリアルを使用してください。
また、tableView:heightForRowAtIndexPath:メソッドを使用してセルの高さを設定しますindexpath
于 2012-11-02T06:37:15.767 に答える
3
これは私の前の答えからです-UITableViewCellの高さのカスタマイズ:
このメソッドを使用して、テキストのテキストの高さを取得します
-(CGFloat)getLabelHeightForText:(NSString *)text andWidth:(CGFloat)labelWidth
{
CGSize maximumSize = CGSizeMake(labelWidth, 10000);
//provide appropriate font and font size
CGSize labelHeighSize = [text sizeWithFont: [UIFont fontWithName:@"Trebuchet MS" size:13.0f]
constrainedToSize:maximumSize
lineBreakMode:UILineBreakModeTailTruncation];
return labelHeighSize.height;
}
このメソッドは、渡すテキストの高さを返します。このメソッドをクラスに追加します。そして、 tableView:heightForRowAtIndexPath:デリゲートメソッドを使用して、各セルの高さを設定します
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
Feedback *item = [self.items objectAtIndex:indexPath.row];
CGFloat textHeight = [self getLabelHeightForText:item.comment andWidth:162];//give your label width here
return textHeight;
}
于 2012-11-02T06:37:01.230 に答える
2
編集して試すことができるサンプル
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *itemAtIndex = (NSDictionary *)[chatQuestions objectAtIndex:indexPath.row];
if ([self sizeForText:[itemAtIndex objectForKey:@"text"]].height+20>50) {
return [self sizeForText:[itemAtIndex objectForKey:@"text"]].height+20;
}
else{
return 50;}
}
-(CGSize)sizeForText:(NSString*)text
{
CGSize constraintSize;
constraintSize.width = 190.0f;
constraintSize.height = MAXFLOAT;
UIFont *labelFont = [UIFont fontWithName:@"Noteworthy-Light" size:18];
CGSize stringSize =[text sizeWithFont:labelFont constrainedToSize: constraintSize lineBreakMode: UILineBreakModeWordWrap];
return stringSize;
}
于 2012-11-02T06:39:24.163 に答える
1
//Calculate the expected size based on the font and linebreak mode of your label
CGSize maximumLabelSize = CGSizeMake(296,9999);
CGSize expectedLabelSize = [yourString sizeWithFont:yourLabel.font constrainedToSize:maximumLabelSize lineBreakMode:yourLabel.lineBreakMode];
//adjust the label the the new height.
CGRect newFrame = yourLabel.frame;
newFrame.size.height = expectedLabelSize.height;
yourLabel.frame = newFrame;
于 2012-11-02T06:39:23.883 に答える