我有一個正確的細節單元格佈局的分組tableview。現在我從我的服務器上取回數據。 有時候,我碰到了一個非常大的文本。所以tableviewCell應該比別人高。你可以在這裏看到我的問題的截圖。取決於文本的UITableViewCell的動態高度
就像你可以在我的第一個單元格中看到我有一個非常大的文本。現在我希望單元格的高度隨着文本的長度而增長,所以我總是可以看到整個文本而不是'...'。這就是我在代碼中的時刻。
#define PADDING 10.0f
- (CGFloat)tableView:(UITableView *)t heightForRowAtIndexPath:(NSIndexPath *)indexPath {
Subscription *sub = [_dictTask valueForKeyPath:@"subscription"];
NSArray *meta = sub.meta.allObjects;
NSString *text = [[meta objectAtIndex:indexPath.row]valueForKey:@"sum_value"];
CGSize textSize = [text sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:CGSizeMake(self.tableView.frame.size.width - PADDING * 3, 1000.0f)];
return textSize.height + PADDING * 3;
}
編輯 代碼,我的時刻。
- (CGFloat)tableView:(UITableView *)t heightForRowAtIndexPath:(NSIndexPath *)indexPath {
Subscription *sub = [_dictTask valueForKeyPath:@"subscription"];
NSArray *meta = sub.meta.allObjects;
NSString *text = [[meta objectAtIndex:indexPath.row]valueForKey:@"sum_value"];
UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:14.0];
CGSize constraintSize = CGSizeMake(330.0f, 400); // Make changes in width as per your label requirement.
CGSize textSize = [text sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
return textSize.height;
}
這就是結果。
編輯的cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
Subscription *sub = [_dictTask valueForKeyPath:@"subscription"];
NSArray *meta = sub.meta.allObjects;
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text = [[meta objectAtIndex:indexPath.row]valueForKey:@"sum_label"];
cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.detailTextLabel.numberOfLines = 0;
cell.detailTextLabel.text = [[meta objectAtIndex:indexPath.row]valueForKey:@"sum_value"];
cell.textLabel.textColor = [UIColor colorWithRed:102/255.0
green:102/255.0
blue:102/255.0
alpha:1.0];
cell.textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:18];
return cell;
}
什麼問題? – robhayward
+1爲你的努力。儘管之前提出了很多類似的問題。 ;-) –
'CGSizeMake(330.0f,400);'嘗試'MAXFLOAT'而不是400,並讓我知道會發生什麼。 –