ios – 如何在单击TableViewCell中的按钮时增加标签和单元格大小.

前端之家收集整理的这篇文章主要介绍了ios – 如何在单击TableViewCell中的按钮时增加标签和单元格大小.前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在单元格上单击seeMoreBtn后扩展单元格大小.

标签和单元格具有不同的长度,但它们是标签大小的约束.

当状态太大时,我添加了一个seeMoreBtn,点击后查看更多剩余文本将在下面显示,然后如何增加标签和单元格大小.

  1. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
  2. {
  3.  
  4.  
  5. NSString *text = [items objectAtIndex:[indexPath row]];
  6. CGSize constraint = CGSizeMake(300.0f,150.0f);
  7. CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:constraint lineBreakMode:NSLineBreakByCharWrapping];
  8.  
  9. CGFloat height1 = MAX(size.height,44.0f);
  10. return height1 + (40.0f);
  11. }
  12. - (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
  13. {
  14. NSString *CellIdentifier = [NSString stringWithFormat:@"Cell-%d",indexPath.row];
  15.  
  16. cell=[tv dequeueReusableCellWithIdentifier:CellIdentifier];
  17.  
  18. if (cell == nil)
  19. {
  20. cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  21. }
  22.  
  23. int lbltag = 1000;
  24.  
  25. label=[[UILabel alloc]initWithFrame:CGRectZero];
  26. [label setLineBreakMode:NSLineBreakByWordWrapping];
  27. [label setMinimumScaleFactor:14.0f];
  28. [label setNumberOfLines:0];
  29. [label setFont:[UIFont systemFontOfSize:14.0f]];
  30. NSString *text = [items objectAtIndex:[indexPath row]];
  31. [label setText:text];
  32. label.tag = lbltag;
  33. [cell addSubview:label];
  34.  
  35. CGSize constraint1=CGSizeMake(300.0f,150.0f);
  36. CGSize size1=[text sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:constraint1 lineBreakMode:NSLineBreakByWordWrapping];
  37.  
  38.  
  39. [label setFrame:CGRectMake(10.0f,10.0f,300.0f,MAX(size1.height,44.0f))];
  40.  
  41. int countText=text.length;
  42.  
  43. if (countText>=350) {
  44. seeMoreBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
  45. [seeMoreBtn setTitle:@"See More" forState:UIControlStateNormal];
  46. seeMoreBtn.frame=CGRectMake(220.0f,44.0f)-10,80.0f,20.0f);
  47. seeMoreBtn.tag=indexPath.row ;
  48. [seeMoreBtn addTarget:self action:@selector(increaseSize:) forControlEvents:UIControlEventTouchUpInside];
  49. [cell addSubview:seeMoreBtn];
  50. }
  51. return cell;
  52. }
  53.  
  54. -(void)increaseSize:(UIButton *)sender{
  55. //What to write here that can adjust the label and cell size
  56.  
  57. }

解决方法

如果您继承UITableViewCell并使用layoutSubviews调整单元格的大小,那会更好.
  1. //In SMTableViewCell.h
  2.  
  3. @interface SMTableViewCell : UITableViewCell
  4.  
  5. @property (weak,nonatomic) IBOutlet UILabel *statusLabel;
  6. @property (weak,nonatomic) IBOutlet UIButton *seeMoreButton;
  7.  
  8. //SMTableViewCell.m
  9.  
  10. - (void)layoutSubviews
  11. {
  12. CGRect labelFrame = self.statusLabel.frame;
  13. labelFrame.size.height = self.frame.size.height - 55.0f;
  14. self.statusLabel.frame = labelFrame;
  15.  
  16. CGRect buttonFrame = self.seeMoreButton.frame;
  17. buttonFrame.origin.y = labelFrame.origin.y+labelFrame.size.height+10.0f;
  18. self.seeMoreButton.frame = buttonFrame;
  19. }

保留一个数组来存储selectedIndexPaths:

  1. @property (nonatomic,strong) NSMutableArray *selectedIndexPaths;

计算单元格的高度:

  1. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
  2. {
  3. BOOL isSelected = [self.selectedIndexPaths containsObject:indexPath];
  4.  
  5. CGFloat maxHeight = MAXFLOAT;
  6. CGFloat minHeight = 40.0f;
  7.  
  8. CGFloat constrainHeight = isSelected?maxHeight:minHeight;
  9. CGFloat constrainWidth = tableView.frame.size.width - 20.0f;
  10.  
  11. NSString *text = self.items[indexPath.row];
  12. CGSize constrainSize = CGSizeMake(constrainWidth,constrainHeight);
  13. CGSize labelSize = [text sizeWithFont:[UIFont systemFontOfSize:15.0f]
  14. constrainedToSize:constrainSize
  15. lineBreakMode:NSLineBreakByCharWrapping];
  16.  
  17. return MAX(labelSize.height+75,100.0f);
  18.  
  19. }

初始化自定义显示更多TableViewCell:

  1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  2. {
  3. static NSString *cellIdentifier = @"CellIdentifier";
  4.  
  5. SMTableViewCell *cell= (SMTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
  6.  
  7. if (cell == nil)
  8. {
  9. cell = [[[NSBundle mainBundle]loadNibNamed:NSStringFromClass([SMTableViewCell class])
  10. owner:nil
  11. options:nil] lastObject];
  12. }
  13.  
  14. BOOL isSelected = [self.selectedIndexPaths containsObject:indexPath];
  15. cell.statusLabel.numberOfLines = isSelected?0:2;
  16.  
  17. NSString *text = self.items[indexPath.row];
  18. cell.statusLabel.text = text;
  19.  
  20.  
  21. NSString *buttonTitle = isSelected?@"See Less":@"See More";
  22. [cell.seeMoreButton setTitle:buttonTitle forState:UIControlStateNormal];
  23. [cell.seeMoreButton addTarget:self action:@selector(seeMoreButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
  24. [cell.seeMoreButton setTag:indexPath.row];
  25.  
  26. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  27.  
  28. return cell;
  29. }

按钮点击事件方法

  1. - (void)seeMoreButtonPressed:(UIButton *)button
  2. {
  3. NSIndexPath *indexPath = [NSIndexPath indexPathForRow:button.tag inSection:0];
  4. [self addOrRemoveSelectedIndexPath:indexPath];
  5. }
  6.  
  7. - (void)addOrRemoveSelectedIndexPath:(NSIndexPath *)indexPath
  8. {
  9. if (!self.selectedIndexPaths) {
  10. self.selectedIndexPaths = [NSMutableArray new];
  11. }
  12.  
  13. BOOL containsIndexPath = [self.selectedIndexPaths containsObject:indexPath];
  14.  
  15. if (containsIndexPath) {
  16. [self.selectedIndexPaths removeObject:indexPath];
  17. }else{
  18. [self.selectedIndexPaths addObject:indexPath];
  19. }
  20.  
  21. [self.tableView reloadRowsAtIndexPaths:@[indexPath]
  22. withRowAnimation:UITableViewRowAnimationFade];
  23.  
  24. }

如果选择了单元格,则会给出相同的事件:

  1. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  2. {
  3. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  4.  
  5. [self addOrRemoveSelectedIndexPath:indexPath];
  6.  
  7. }

样品Demo project link.

猜你在找的iOS相关文章