Jam*_*mes 4 uitableview uilabel ios
无论设备或方向如何,我都试图让单元格中的标签尺寸合适.我能够正确地调整行高.我也可以正确设置标签高度cellForRowAtIndexPath,并可以在我的日志中检查.但是,到达时willDisplayRowAtIndexPath,标签高度已经改变,但只有当电池不是320磅宽时.
这是我的代码 -
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CustomCellIdentifier";
CustomInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil){
cell = [[CustomInfoCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
NSArray *objects = [[NSBundle mainBundle] loadNibNamed:@"CustomInfoCell" owner:self options:nil];
cell = objects[0];
}
// Configure the cell...
cell.customTitleLabel.text = [_data[indexPath.row] objectForKey:t];
CGFloat labelWidth = self.view.frame.size.width-40;
NSLog(@"labelWidth:%f",labelWidth);
NSString *text = [_data[indexPath.row] objectForKey:d];//correct text
CGSize labelsize=[text sizeWithFont:cell.customDetailLabel.font constrainedToSize:CGSizeMake(labelWidth, 2000.0) lineBreakMode:cell.customDetailLabel.lineBreakMode];
NSLog(@"labelsize:%f,%f",labelsize.width,labelsize.height);
//For testing
cell.customDetailLabel.backgroundColor = [UIColor redColor];
NSLog(@"Pre: %@",cell.customDetailLabel);
cell.customDetailLabel.frame=CGRectMake(20, 22, labelWidth, labelsize.height);
cell.customDetailLabel.text = text;
NSLog(@"Post: %@",cell.customDetailLabel);
return cell;
}
Run Code Online (Sandbox Code Playgroud)
在willDisplayRowAtIndexPath我也打印标签信息.这是一行打印的内容 -
2013-03-24 18:33:44.009 Bridge Alert[57793:1e503] labelWidth:728.000000
2013-03-24 18:33:44.010 Bridge Alert[57793:1e503] labelsize:713.000000,76.000000
2013-03-24 18:33:44.010 Bridge Alert[57793:1e503] Pre: <UILabel: 0xad3eaf0; frame = (20 20; 280 21); text = 'Detail'; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x17372eb0>>
2013-03-24 18:33:44.011 Bridge Alert[57793:1e503] Post: <UILabel: 0xad3eaf0; frame = (20 22; 728 76); text = 'Detail'; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x17372eb0>>
2013-03-24 18:33:44.011 Bridge Alert[57793:1e503] Text set: <UILabel: 0xad3eaf0; frame = (20 22; 728 76); text = 'A bridge is considered “f...'; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x17372eb0>>
2013-03-24 18:33:44.014 Bridge Alert[57793:1e503] Display:<UILabel: 0xad3eaf0; frame = (20 20; 728 190); text = 'A bridge is considered “f...'; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x17372eb0>>
Run Code Online (Sandbox Code Playgroud)
如您所见,通过Display,标签会调整大小.我假设高度是以某种方式重新计算的,基于单元格是否为320 pt宽,这是内置的UITableViewCell宽度.
如何才能使标签尺寸正确?
Pau*_*ida 19
这是使用AutoLayout尽可能使用最少代码对我有用的东西.
在我的ViewController中,我添加了以下方法
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString * yourText = //place here the text you want to calculate its size;
return 40 + [self heightForText:yourText];
}
-(CGFloat)heightForText:(NSString *)text
{
NSInteger MAX_HEIGHT = 2000;
UITextView * textView = [[UITextView alloc] initWithFrame: CGRectMake(0, 0, 320, MAX_HEIGHT)];
textView.text = text;
textView.font = [UIFont boldSystemFontOfSize:12];
[textView sizeToFit];
return textView.frame.size.height;
}
Run Code Online (Sandbox Code Playgroud)
上面的代码基本上根据我的UILabel的大小+填充数来改变UITableViewCell的大小,在我的例子中我定义为40.
在此之后,我必须添加一些约束,如下所示,以便使UILabel"拉伸"为UITableViewCell

运行后:

注意:我从SO中的许多线程中获取了不同的代码片段来提出这个解决方案.我希望我能记住这里提到的所有人
希望它适合你们.
干杯
| 归档时间: |
|
| 查看次数: |
9513 次 |
| 最近记录: |