Ale*_*der 11 objective-c uitableview ios
我需要在UITableViewStyleGrouped表中创建自定义单元格.如果在没有截断的情况下显示textLabel和detailTextLabel,则必须看起来像UITableViewCellStyleValue1;如果在Value1样式中标签之一,则必须看作UITableViewCellStyleSubtitle(但是带有detailTextLabel width = contentView.frame.size.width和UITextAlignmentRight)被截断.
我需要在方法中知道cell.contentView的宽度,- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath以确定单元格的高度.我比较了cell.contentView.width和标签宽度之和(我得到它们使用方法sizeThatFits:)来决定它.
那么,如何在创建单元格之前获得正确的cell.contentView.frame.size.width?
UPD:一些代码来澄清问题.
- (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;
}
Run Code Online (Sandbox Code Playgroud)
UPD2:问题是,当我创建单元格时,cell.contentView.frame.size.width等于cell.frame.size.width并且等于320,但是在单元格出现后,cell.contentView.frame.size.width会依赖于tableView而发生变化width和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;
}
Run Code Online (Sandbox Code Playgroud)
Mya*_*onn -2
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0)
return 60.0f;
else
return 50.0f;
}
Run Code Online (Sandbox Code Playgroud)
通过应用它,您可以设置单元格 - > contentView。如果您需要动态单元格宽度,请使用上述方法中的标志
| 归档时间: |
|
| 查看次数: |
12563 次 |
| 最近记录: |