根据目标c中的标签文本更改tableview单元格的高度

时间:2016-02-20 06:35:55

标签: ios objective-c uitableview

我正在制作一个简单的基于地图的应用程序。表视图的自定义单元格中有两个标签。标签的一个文本是动态的,我想根据标签的文字和高度更改单元格的高度。

3 个答案:

答案 0 :(得分:0)

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
             NSMutableString *strauditType = @"Your String ";

            CGRect textRect = [strauditType boundingRectWithSize:maximumLabelSize
                                                         options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
                                                      attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14.0]}
                                                         context:nil];
            CGSize messageSize = textRect.size;
            return messageSize.height  + 10.0f; 

}

答案 1 :(得分:0)

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewAutomaticDimension;
}

答案 2 :(得分:0)

我尝试了解决方案。

ViewController.m

#import "ViewController.h"
#import "CustomTableViewCell.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
   NSMutableArray *array;
}

@property (strong, nonatomic) IBOutlet UITableView *tableViewIncreaseRowHeight;

@end

@implementation ViewController

- (void)viewDidLoad 
{
   [super viewDidLoad];
   // Do any additional setup after loading the view, typically from a nib.

   UIFont *labelFont = [UIFont fontWithName:@"Noteworthy-Bold" size:20];

   NSDictionary *arialdict = [NSDictionary dictionaryWithObject:labelFont forKey:NSFontAttributeName];

   NSMutableAttributedString *message = [[NSMutableAttributedString alloc] initWithString:@"this is just the sample example of how to calculate the dynamic height for tableview cell which is of around 7 to 8 lines. you will need to set the height of this string first, not seems to be calculated in cellForRowAtIndexPath method." attributes:arialdict];

   array = [NSMutableArray arrayWithObjects:message, nil];

   NSMutableAttributedString *message_1 = [[NSMutableAttributedString alloc] initWithString:@"you will need to set the height of this string first, not seems to be calculated in cellForRowAtIndexPath method." attributes:arialdict];

   [array addObject:message_1];

}

- (void)didReceiveMemoryWarning 
{
  [super didReceiveMemoryWarning];
  // Dispose of any resources that can be recreated.
}

//table view delegate and data source methods  
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
   return 1;
}

 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 {
    return 1;
 }
 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
    CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableHeaderFooterViewWithIdentifier:@"cell"];
    NSArray *arr = [[NSBundle mainBundle]loadNibNamed:@"CustomTableViewCell" owner:self options:nil];
    if(cell==nil)
    {
       cell = arr[0];
    }

    cell.labelName.attributedText = array[indexPath.row] ;
    cell.labelName.numberOfLines = 2;
    [cell.labelName sizeToFit];
    cell.labelID.text = @"20";
    return cell;
  }

-(float)height :(NSMutableAttributedString*)string
{
    NSAttributedString *attributedText = string;
    //you need to specify the some width, height will be calculated
    CGRect rect = [attributedText boundingRectWithSize:(CGSize){225, MAXFLOAT}
                                           options:NSStringDrawingUsesLineFragmentOrigin
                                           context:nil];

   CGSize requiredSize = rect.size;
   //finally u return your height  
   return requiredSize.height; 
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   //whatever the height u need to calculate calculate hear only
   CGFloat heightOfcell = [self height:array[indexPath.row]];
   NSLog(@"%f",heightOfcell);
   return heightOfcell;
}
@end

积分转到

Source

非常感谢你: - )