我有一个带有自定义单元格的主UITableView。此主表视图中单元格的高度是使用自动布局约束动态计算的。现在我想在主表视图的单元格中添加slave UITableView。请提供有关如何实现此目的的说明。 (示例项目也可能非常有用)。
答案 0 :(得分:0)
找到了解决方案。
#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