简体   繁体   English

如何以编程方式将约束添加到UITableViewCell的默认imageView和textLabel

[英]How to add Constraints programmatically to default imageView and textLabel of UITableViewCell

I am currently creating my cell like this 我目前正在这样创建我的手机

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;

Everything is working fine but some of my text which is long gets truncated at the end. 一切正常,但是我的一些很长的文本在结尾处被截断。 How can i avoid that?? 我如何避免这种情况? I don't want to set the numberOfLines property. 我不想设置numberOfLines属性。

Is there a way i can arrange my imageView and textlabel a bit via constraints so that my text looks fine. 有没有办法我可以通过约束来安排我的imageView和textlabel,使我的文本看起来很好。 I tried adding constraints to contentView of cell but that make my cell all weird. 我尝试将约束添加到单元的contentView,但是这使我的单元全部变得怪异。

I am new to iOS development and learning. 我是iOS开发和学习的新手。 Any help will be greatly appreciated. 任何帮助将不胜感激。

If you don't want to set numberOfLines . 如果您不想设置numberOfLines adjustsFontSizeToFitWidth property will set your content bound to width without truncating it. adjustsFontSizeToFitWidth属性将设置内容绑定到width而不截断它。 but it will decrease font size if required. 但如果需要,它将减小字体大小。

cell.textLabel.adjustsFontSizeToFitWidth = true

There is no need to add constraint programmatically, Step 1 - Drag and drop the cell on UITableview Step 2 - Design your cell with image view and label , use constraints Step 3 - Create CustomCell.h and CustomCell.m file which will be inherited from UITableViewCell Step 4 - create IBOutlet for image view and label in CustomCell.h Step 5 - In your ViewController - 无需以编程方式添加约束,步骤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 https://www.raywenderlich.com/129059/self-sizing-table-view-cells

UIImageView *imgarrow=[[UIImageView alloc]init ]; UIImageView * imgarrow = [[UIImageView alloc] init];

imgarrow.frame=CGRectMake(230,2, 27, 27); imgarrow.frame = CGRectMake(230,2,27,27);

imgarrow.image=[UIImage imageNamed:@"check_mark@2x.png"]; imgarrow.image = [UIImage imageNamed:@“ check_mark@2x.png”];

[cell addSubview:imgarrow]; [cell addSubview:imgarrow];

//write same code for UILabel //为UILabel编写相同的代码

//change rectmake values //更改rectmake值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何以编程方式将约束添加到tableView单元格的默认textLabel和annexView - How to Add constraints programmatically to default textLabel and accessoryView of tableView cell UITableViewCell-如何更改selectedBackgroundView,默认ImageView,textLabel,detailTextLabel框架 - UITableViewCell- how to change selectedBackgroundView, default ImageView, textLabel, detailTextLabel frames iOS-如何计算UITableViewCell的默认textLabel的宽度? - iOS - How to calc width of a default textLabel of UITableViewCell? 如何以编程方式在UITableViewCell swift中为UIStackView添加约束? - How to add constraints to UIStackView inside of a UITableViewCell swift programmatically? 如何在iOS的UITableViewCell中获取默认textLabel的大小? - How to get size of default textLabel inside UITableViewCell in iOS? 如何在UITableViewCell中更改textLabel的宽度 - How to change the width of the textLabel in a UITableViewCell 以编程方式在UITableViewCell上设置约束 - Programmatically setting constraints on UITableViewCell UITableViewCell缩进仅适用于默认单元格textLabel - UITableViewCell indentation works only for default cell textLabel 在默认的UITableViewCell中显示一个imageView - Display an imageView in the default UITableViewCell 如何以编程方式为Rangeslider添加约束? - how to add constraints programmatically for rangeslider?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM