判断label有几行

先计算总文字的高度,然后再除以每行文字的高度。

总文字的高度:CGFloat  textH = [label.text  boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size.height;

每行文字的高度:CGFloat lineHeight = label.font.lineHeight;

行数:NSInteger  lineCount = textH / lineHeight;


//动态改变cell高度

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

#warning Incomplete implementation, return the number of sections

return 1;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

#warning Incomplete implementation, return the number of rows

return 100;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

return cell;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

if(self.index == indexPath){

return 120;

}

return 60;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

self.index = indexPath;

[tableView deselectRowAtIndexPath:indexPath animated:TRUE];

// 重点是这2句代码实现的功能

[tableView beginUpdates];

[tableView endUpdates];

}

你可能感兴趣的:(判断label有几行)