Reputation: 2344
The code in questions is below:
-(void)addSpeedTable {
self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"newCell"];
self.tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
self.tableView.rowHeight = 45;
self.tableView.sectionFooterHeight = 22;
self.tableView.sectionHeaderHeight = 22;
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView = _tableView;
NSLog(@"TableView Frame %@", NSStringFromCGRect(self.tableView.frame));
[self.view addSubview:self.tableView];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)theTableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)theTableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"newCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"newCell"];
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
cell.textLabel.text = @"Test";
return cell;
}
As you can see if I have a section set to 1 and row set to 1, yet when the view is added the table takes up the entire screen, which I assume is because I initiate with a frame of the view bounds.
Is there any way to force the table to only be the size of the rows required?
My plan is to add more than one table to this single view and I want them appearing one after the other along with title labels and a scrollview. I want the height to be decided by the number of rows.
Thanks
UPDATED CODE THANKS TO THE CORRECT ANSWER:
-(void)addSpeedTable {
CGRect frame = self.tableView.frame;
frame.size.height = (40 * resultHeadings.count)+35;
frame.size.width = self.view.frame.size.width;
//If more than one table has been shown increase Y axis height for view
if(tablesDisplayed > 0) {
NSLog(@"Tables displayed greater than zero");
viewHeight = viewHeight + frame.size.height;
}
frame.origin.y = viewHeight;
frame.origin.x = 0;
self.tableView = [[UITableView alloc] initWithFrame:frame];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"newCell"];
self.tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
self.tableView.rowHeight = 40;
self.tableView.sectionFooterHeight = 0;
self.tableView.sectionHeaderHeight = 22;
self.tableView.scrollEnabled = NO; // Disable ScrollView
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView = _tableView;
NSLog(@"TableView Frame %@", NSStringFromCGRect(frame));
[self.view addSubview:self.tableView];
tablesDisplayed++;
}
As I'm adding more than one table in the long run I calculate the height based on a calculation. This works great.
Upvotes: 0
Views: 161
Reputation: 2377
Here are two things you could try. Without knowing where addSpeedTable
is called from, I'd err on putting this in your cellForRowAtIndexPath
method.
Set the height to the self.tableView.contentHeight
CGRect frame = self.tableView.frame;
frame.size.height = self.tableView.contentSize.height;
self.tableView.frame = frame;
Call [self.tableView sizeToFit]
Upvotes: 1