tôi cần phải làm cho tế bào tùy chỉnh trong UITableViewStyleGrouped table.It phải trông như UITableViewCellStyleValue1 nếu textLabel và detailTextLabel được thể hiện mà không cần cắt ngắn, hoặc như UITableViewCellStyleSubtitle (nhưng với detailTextLabel width = contentView.frame.size.width và UITextAlignmentRight) nếu trong Value1 phong cách một trong các nhãn được cắt ngắn.Cách lấy chiều rộng contentView của UITableViewCell?
Tôi cần biết chiều rộng của cell.contentView trong phương thức - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
để quyết định chiều cao của ô là bao nhiêu. Tôi so sánh cell.contentView.width và tổng chiều rộng nhãn (tôi nhận được chúng bằng cách sử dụng phương pháp sizeThatFits:
) để quyết định nó.
Vì vậy, Cách lấy đúng cell.contentView.frame.size.width trước khi tạo ô?
UPD: một số mã để làm rõ câu hỏi.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat cellHeight = 44;
CGSize textSize = [[arrayOfTextData objectAtIndex:indexPath.row]
sizeWithFont:[UIFont systemFontOfSize:14]
constrainedToSize:CGSizeMake(1000, 100)
lineBreakMode:UILineBreakModeCharacterWrap];
CGSize detailedTextSize = [[arrayOfDetailTextData objectAtIndex:indexPath.row]
sizeWithFont:[UIFont systemFontOfSize:14]
constrainedToSize:CGSizeMake(1000, 100)
lineBreakMode:UILineBreakModeCharacterWrap];
if (textSize.width + detailedTextSize.width > /* I need here cell.contentView.frame.size.width*/ 300) {
cellHeight = textSize.height + detailedTextSize.height;
}
return cellHeight;
}
UPD2: Vấn đề là khi tôi tạo tế bào các cell.frame.size.width bằng cell.contentView.frame.size.width và bình đẳng 320, nhưng sau khi tế bào xuất hiện cell.contentView.frame Các thay đổi .size.width phụ thuộc vào chiều rộng tableView và kiểu tableView.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"tmpCell";
CustomTableViewCellValue1OrSubtitle *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CustomTableViewCellValue1OrSubtitle alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
//--Configure the cell
cell.textLabel.text = [arrayOfTextData objectAtIndex:indexPath];
cell.detailTextLabel.text = [arrayOfDetailTextData objectAtIndex:indexPath.row];
[cell layoutIfNeeded];
CGSize textSize = [cell.textLabel.text sizeWithFont:cell.textLabel.font constrainedToSize:CGSizeMake(cell.contentView.bounds.size.width, 99999) lineBreakMode:cell.textLabel.lineBreakMode];
CGSize detailedTextSize = [cell.detailTextLabel.text sizeWithFont:cell.textLabel.font constrainedToSize:CGSizeMake(cell.contentView.bounds.size.width, 99999) lineBreakMode:cell.textLabel.lineBreakMode];
CGFloat cellHeight = cell.contentView.frame.size.height;
NSLog(@"contentView.frame.size.width = %f",cell.contentView.frame.size.width);
//out: contentView.frame.size.width = 320.0
//Always print 320, even if I set Simulator on iPad
if (textSize.width + detailedTextSize.width > cell.contentView.frame.size.width) {
cellHeight = textSize.height + detailedTextSize.height;
}
return cellHeight;
}
Bạn đã bao giờ tìm được giải pháp cho điều này chưa? Tôi có một vấn đề tương tự khi 'contentView.frame.size.width' trả về 320 trên iPhone trong khi khởi tạo. – cod3monk3y
Tôi đã kết thúc triển khai 'layoutSubviews' để thay đổi kích thước nội dung ô tùy chỉnh của mình. – cod3monk3y
Tôi đã làm điều tương tự. – Alexander