[英]How to Add constraints programmatically to default textLabel and accessoryView of tableView cell
[英]How to add Constraints programmatically to default imageView and textLabel of UITableViewCell
我目前正在这样创建我的手机
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
cell.imageView.image = [UIImage imageNamed:image];
cell.imageView.tintColor = [UIColor whiteColor];
cell.textLabel.text = @"Push me";
cell.textLabel.textColor = [UIColor whiteColor];
cell.userInteractionEnabled = YES;
return cell;
一切正常,但是我的一些很长的文本在结尾处被截断。 我如何避免这种情况? 我不想设置numberOfLines属性。
有没有办法我可以通过约束来安排我的imageView和textlabel,使我的文本看起来很好。 我尝试将约束添加到单元的contentView,但是这使我的单元全部变得怪异。
我是iOS开发和学习的新手。 任何帮助将不胜感激。
如果您不想设置numberOfLines
。 adjustsFontSizeToFitWidth
属性将设置内容绑定到width而不截断它。 但如果需要,它将减小字体大小。
cell.textLabel.adjustsFontSizeToFitWidth = true
无需以编程方式添加约束,步骤1-将单元格拖放到UITableview上步骤2-使用图像视图和标签设计单元格,使用约束条件步骤3-创建CustomCell.h和CustomCell.m文件UITableViewCell步骤4-在CustomCell.h中为图像视图和标签创建IBOutlet步骤5-在ViewController中-
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCell"];
cell.textLabel.text = [self.dataAraay objectAtIndex:indexPath.row];
return cell;
}
https://www.raywenderlich.com/129059/self-sizing-table-view-cells
UIImageView * imgarrow = [[UIImageView alloc] init];
imgarrow.frame = CGRectMake(230,2,27,27);
imgarrow.image = [UIImage imageNamed:@“ check_mark@2x.png”];
[cell addSubview:imgarrow];
//为UILabel编写相同的代码
//更改rectmake值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.