[英]Multiple lines of a label in a custom UITableviewCell
我一直在尋找我的問題的任何提示。 但我無法找到解決方案。
我已經創建了UITableviewCell(FeedCell)的子類。 有一個圖像和兩個標簽。 問題是我需要多線的標簽不會出現多線。
我用autolayot。
這是一個顯示用戶twitterfeed的應用程序。
我的代碼:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
FeedCell *tweetCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (tweetCell == nil) {
tweetCell = [[FeedCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
[tweetCell.tweetText setNumberOfLines:0];
[tweetCell.tweetText setLineBreakMode:NSLineBreakByWordWrapping];
[tweetCell.tweetText setFont:[self fontForCell] ];
}
NSDictionary *tweet = _dataSource[[indexPath row]];
NSString *tweetString = [tweet valueForKey:@"text"];
tweetCell.name.text =[tweet valueForKeyPath:@"user.name"];
[tweetCell.tweetText setText:tweetString];
return tweetCell;
}
我還設置了heigthforRowAtIndexPath:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *tweet = _dataSource[[indexPath row]];
NSString *theText=[tweet valueForKey:@"text"];
UIFont *cellFont = [self fontForCell];
CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
CGSize labelSize = [theText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];
return labelSize.height + 20;
}
問題是tweet cell.tweetText沒有顯示多行。 我沒有嘗試過另一個CellStyle(我使用自定義cellstyle)。
有人提示嗎?
對於多行使用以下內容:
tweetCell.tweetText.numberOfLines = 0;
[tweetCell.tweetText sizeToFit];
出於測試目的,在以下方法中將行的高度設置為46.0f:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
我無法修復高度問題,但這確實給了我一個多線的UILabel
我知道這是一個很老的帖子,但是在我搜索的時候就出現了。
我通過http://www.raywenderlich.com/73602/dynamic-table-view-cell-height-auto-layout得到了這樣的例子。
我認為對於iOS8,需要以下內容:
嘗試
[tweetCell.tweetText sizeToFit]
首先,如果要顯示2行文本(最小1和最大2),則numberOfLines
必須設置為2.將其設置為0表示沒有限制。
其次,僅設置行數是不夠的。 要指定的標簽寬度HAS。 使用sizeToFit
或設置常量值。
嘗試使用設置這些花括號的行數,換行符和字體OUTSIDE的代碼
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.