I have a few (grouped style) tables in my iphone app (only on part of the screen and added with Interface Builder
though, not subclassed from UITableViewController
) that 80% of the time are small and will fit on the screen. When the table fits on the screen, I'd like to disable scrolling, to make it a bit cleaner. But if the table goes off the screen (when rows are later added to it), I'd like to enable scrolling again (because otherwise you can't see that content.)
Is there a way to do this? I can't seem to figure it out. I do know to do:
tableView.scrollEnabled = NO;
but I'm not sure where, or if I have to calculate the table object size or something to get this to work.
Update: Here's the solution that finally worked for me:
if (table.contentSize.height < table.frame.size.height) {
table.scrollEnabled = NO;
}
else {
table.scrollEnabled = YES;
}
I run this code after calling reloadData
on the table, and it calculates the right sizes and appears to work.
table.frame.size.height
is the actual size of the object (you can see this in Interface Builder
) displayed on the screen, whereas table.contentSize.height
is the heights of: the header, the footer, and the height of every cell added together.
You can verify the number of visible cells using this function:
This method will return an array with the cells that are visible, so you can count the number of objects in this array and compare with the number of objects in your table.. if it's equal.. you can disable the scrolling using:
As @Ginny mentioned.. we would can have problems with partially visible cells, so this solution works better in this case:
In case you are using autoLayout this solution do the job:
I think you want to set
// Enable scrolling based on content height self.tableView.scrollEnabled = table.contentSize.height > table.frame.size.height;