我有分段和行的tableview。当我点击各个部分时,行会显示当我再次点击该部分时,行将消失。
在扩展行我有视图我有2个标签,其中高度是动态的,我正在使用自动调整大小。我正在计算2个标签hight。我正在计算标签的高度和取决于我我的排名很高。
当单击该部分时加载行时,显示完美高度的单元格。(注意行indexpath == 0我正在采取一个高度,其他我采取同样的高度。)
当我点击某个索引路径的扩展单元格中的按钮,表示indexpath == 1时,我正在重新加载第一个索引路径。
添加按钮下的代码。
NSIndexPath* rowToReload = [NSIndexPath indexPathForRow:sender.tag
inSection:sectionselect];
NSLog(@"%@",rowToReload);
NSArray* rowsToReload = [NSArray arrayWithObjects:rowToReload, nil];
[_tblview reloadRowsAtIndexPaths:rowsToReload withRowAnimation:UITableViewRowAnimationNone];
计算高度的代码。
-(CGFloat)getLabelHeightForString: (NSString *)string font: (UIFont *)font
{
CGSize size = CGSizeMake( menudetailscell.typelbl.frame.size.width, MAXFLOAT);
NSStringDrawingContext *context = [[NSStringDrawingContext alloc] init];
CGSize boundingBox = [string boundingRectWithSize:size
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName:font}
context:context].size;
size = CGSizeMake(ceil(boundingBox.width), ceil(boundingBox.height));
return size.height;
}
在indexpath处的hightforrow
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row==0) {
menudetailscell.toplbl.hidden=NO;
NSArray *aarr=[menuarray[indexPath.section] valueForKey:@"menu"];
NSString *namestr=[[aarr objectAtIndex:indexPath.row]valueForKey:@"name"];
UIFont *font=[UIFont fontWithName:@"Montserrat-Regular" size:15.0];
NSString *desc=[[aarr objectAtIndex:indexPath.row]valueForKey:@"description"];
UIFont *desfont=[UIFont fontWithName:@"Rubik-Light" size:13.0];
CGFloat size = [self getLabelHeightForString: namestr font:font];
CGFloat dissize = [self getLabelHeightForString:desc font:desfont];
return 90+size+dissize-40;
}
else{
// menudetailscell.toplbl.hidden=NO;
NSArray *aarr=[menuarray[indexPath.section] valueForKey:@"menu"];
NSString *namestr=[[aarr objectAtIndex:indexPath.row]valueForKey:@"name"];
UIFont *font=[UIFont fontWithName:@"Montserrat-Regular" size:15.0];
NSString *desc=[[aarr objectAtIndex:indexPath.row]valueForKey:@"description"];
UIFont *desfont=[UIFont fontWithName:@"Rubik-Light" size:13.0];
CGFloat size = [self getLabelHeightForString: namestr font:font];
CGFloat dissize = [self getLabelHeightForString:desc font:desfont];
menudetailscell.toplbl.hidden=YES;
return 75+size+dissize-33;
}
}
//cellforrowatindexpath
UIFont *font=[UIFont fontWithName:@"Montserrat-Regular" size:15.0];
CGFloat size = [self getLabelHeightForString: menudetailscell.typelbl.text font:font];
menudetailscell.typelbl.frame=CGRectMake(menudetailscell.typelbl.frame.origin.x, menudetailscell.typelbl.frame.origin.y, menudetailscell.typelbl.frame.size.width, size);
UIFont *desfont=[UIFont fontWithName:@"Rubik-Light" size:13.0];
CGFloat dissize = [self getLabelHeightForString:
menudetailscell.describtionlbl.text font:desfont];menudetailscell.describtionlbl.frame=CGRectMake(menudetailscell.describtionlbl.frame.origin.x, menudetailscell.typelbl.frame.origin.y+size+2, menudetailscell.describtionlbl.frame.size.width, dissize);