实现 UITableView (Slave) 的动态高度,它作为子视图添加到另一个具有动态高度的 UITableView (Master) 的自定义单元格中
Posted
技术标签:
【中文标题】实现 UITableView (Slave) 的动态高度,它作为子视图添加到另一个具有动态高度的 UITableView (Master) 的自定义单元格中【英文标题】:Achieve dynamic height for UITableView (Slave) which is added as subview inside a custom cell of an another UITableView (Master) with dynamic height 【发布时间】:2016-12-05 09:42:08 【问题描述】:我有一个带有自定义单元格的主 UITableView。此主表视图中的单元格高度是使用自动布局约束动态计算的。现在我想在主表视图的单元格中添加从属 UITableView。请提供有关如何实现此目的的说明。 (示例项目也可能很有帮助)。
【问题讨论】:
【参考方案1】:找到了解决办法。
#import "TableViewController.h"
#import "CustomCellTableViewCell.h"
@interface TableViewController ()
NSMutableArray *array;
NSString *cellID;
@end
@implementation TableViewController
- (void)viewDidLoad
[super viewDidLoad];
array = [[NSMutableArray alloc]initWithObjects:@"Hi",@"13.861970 dsdlkdksjdksksdjkljkdlsjkldsjk dsdlkdksjdksksdjkljkdlsjkldsjkdsdlkdksjdksksdjkljkdlsjkldsjkdsdlkdksjdksksdjkljkdlsjkldsjk", @"Latitude",@"100.504250", @"Longitude",@"50 kph", @"Speed",@"1000 meters", @"Altitude",@"12/04/2010", @"Date",@"05:45 PM", @"Time",@"North west", @"Course", nil];
cellID = @"cellID";
[self.tableView setRowHeight:UITableViewAutomaticDimension];
[self.tableView setEstimatedRowHeight:50];
[self.tableView registerClass:[CustomCellTableViewCell class] forCellReuseIdentifier:cellID];
- (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 [array count];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
CustomCellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if (indexPath.row == 1)
[cell addTableWithText:[array objectAtIndex:indexPath.row]];
else
[cell setCellText:[array objectAtIndex:indexPath.row]];
return cell;
CustomCellTableViewCell 实现如下。
#import <UIKit/UIKit.h>
@interface CustomCellTableViewCell : UITableViewCell<UITableViewDelegate,UITableViewDataSource>
UILabel *cellLabel;
UITableView *table;
NSMutableArray *tableData;
-(void) setCellText:(NSString *) text;
-(void) addTableWithText:(NSString *) text;
@end
#import "CustomCellTableViewCell.h"
#import "Masonry.h"
@implementation CustomCellTableViewCell
-(void) setCellText:(NSString *) text
if (cellLabel == nil)
cellLabel = [[UILabel alloc]init];
[cellLabel setNumberOfLines:0];
[cellLabel setBackgroundColor:[UIColor redColor]];
[self addSubview:cellLabel];
[cellLabel mas_makeConstraints:^(MASConstraintMaker *make)
make.edges.equalTo(self).with.insets(UIEdgeInsetsMake(10, 10, 10,10));
];
[cellLabel setText:text];
-(void) addTableWithText:(NSString *) text
tableData = [[NSMutableArray alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9", nil];
if (table == nil)
table = [[UITableView alloc]init];
[table setDelegate:self];
[table setDataSource:self];
[table setScrollEnabled:NO];
[table setEstimatedRowHeight:40];
[table setRowHeight:UITableViewAutomaticDimension];
[self addSubview:table];
[table registerClass:[CustomCellTableViewCell class] forCellReuseIdentifier:@"cell"];
[table mas_makeConstraints:^(MASConstraintMaker *make)
make.top.greaterThanOrEqualTo(self);
make.left.equalTo(self).with.offset(10);
make.right.equalTo(self).with.offset(-10);
make.bottom.equalTo(self).with.offset(-10);
];
if (cellLabel == nil)
cellLabel = [[UILabel alloc]init];
[cellLabel setNumberOfLines:0];
[cellLabel setBackgroundColor:[UIColor redColor]];
[self addSubview:cellLabel];
[cellLabel mas_makeConstraints:^(MASConstraintMaker *make)
make.top.equalTo(self).with.offset(10);
make.left.equalTo(self).with.offset(10);
make.right.equalTo(self).with.offset(-10);
make.bottom.equalTo(table.mas_top).with.offset(-10);
];
else
[cellLabel mas_remakeConstraints:^(MASConstraintMaker *make)
make.top.equalTo(self).with.offset(10);
make.left.equalTo(self).with.offset(10);
make.right.equalTo(self).with.offset(-10);
make.bottom.equalTo(table.mas_top).with.offset(-10);
];
[cellLabel setText:text];
[table layoutIfNeeded];
CGFloat height = table.contentSize.height;
[table mas_remakeConstraints:^(MASConstraintMaker *make)
make.top.greaterThanOrEqualTo(self);
make.left.equalTo(self).with.offset(10);
make.right.equalTo(self).with.offset(-10);
make.bottom.equalTo(self).with.offset(-10);
make.height.equalTo([NSNumber numberWithFloat:height]);
];
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
return 1;
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
return [tableData count];
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
CustomCellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[cell setCellText:[tableData objectAtIndex:indexPath.row]];
return cell;
@end
【讨论】:
以上是关于实现 UITableView (Slave) 的动态高度,它作为子视图添加到另一个具有动态高度的 UITableView (Master) 的自定义单元格中的主要内容,如果未能解决你的问题,请参考以下文章