Getting appropriate UITableViewCell separations: GUI - ios

I have a UITableViewController that is segue-embedded with a UIContainerView. I'm having trouble appropriating the spacings located between the three given UITableViewCell's.
-- I need another space above the very top cell and between the 2nd and 3rd UITableViewCell, I'm having a hard time finding out how to accomplish this.

In your UITableViewDelegate, add this following method:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
From there you can control the total height of each cell independently. Don't do this for large tables, however, as it slows things down.
If you are not using section headers (or footers) already, you can use them to add arbitrary spacing to table cells. Instead of having one section with n rows, create a table with n sections with one row each.
Implement the tableView:heightForHeaderInSection: method to control the spacing.
You may also want to implement tableView:viewForHeaderInSection: to control what the spacing looks like.

Related

IOS/Objective-C: Reusing Cell Throws off Autolayout of Different Size Elements in TableviewCell

I am displaying feed items in a tableview. Depending on the item, the cell must be larger or smaller--for example larger to accomodate a photo or larger still if it references another feed item such as an article and I have to display the item referenced.
In most cases, I've used autolayout to get the cells to auto expand or contract based on the content. However, in a couple cases, this was proving next to impossible so I reset the value of a constraint in code. For example, if there is no photo in one case, I shrink the height of the cell in code as the cell was not shrinking on its own.
This works fine when the page loads. But when you scroll and reuse cells, it leads to weird effects such as the cell expanding vertically or lines being pushed into one another.
Some questions on SO suggest using the method:
-(void) tableView:(UITableView *)tableView didEndDisplayingCell:(IDFeedCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
//Undo changes here
}
The problem is I don't know how to undo changes. For example, if I made a cell smaller by setting
self.height.constant = 100;
Should I set it back to the constant value in storyboard?
What value can I set it to when I don't know what the next item to resuse the cell will need in the way of dimensions.
Thanks for any suggestions. BTW, autolayout shows no issues.

Resize UITableView to fit all cells

I am using a UITableView within an auto-sized UITableViewCell to display a variable, but small number of items. I want the nested UITableView to expand to display all of its rows. (I know this may not be the best way to do things, so please bear with me on the premise.)
The problem I'm having is that the UITableView does not have an intrinsic size based on the size of all of its cells. Is there any way to force all of the cells to load an then compute an intrinsic size? (Or some other way to have a full-sized nested UITableView.)
Thanks!
There is a method called
- (void)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
where you define the height of each cell. I believe the default is 44. You can just take those values and then multiply them by the number of cells you're populating and that will be the height your UITableView should be.

UITableViewCell size does not follow Content View size

I have two UITableViewControllers which use the same set of cell templates, therefore I have created a table view controller in my Storyboard which contains these templates. My table view controllers instantiate an instance of this template holder tableview, then in -tableView:cellForRowAtIndexPath: they deque one of it's cells, and configure it as they need.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *widgetDict = [self.widgets objectAtIndex:indexPath.row];
DMSWidget *widget = [self.widgetStorageTableViewController.tableView dequeueReusableCellWithIdentifier:#"Key Value Row"];
...
return widget;
}
These cells have their inner layouts set up entirely using AutoLayout.
Everything works fine, except that I get cells which have the standard 44 px height and width of the table view, with Content Views narrower than this (depending which they contain, the ones with less contents are smaller), and are longer than the cells themselves. So the Content Views overlap the next cell but don't fill the entire width.
I don't understand, how this could happen. How is it possible, that the UITableViewCell has different size than it's Content View? They should be the same, isn't it?
What I tried to do but failed:
implement -tableView:heightForRowatIndexPath:: tried to return UITableViewAutomaticDimension, but did not help
reload my table view when the sizes are calculated: does nothing
add hacks to cell implementations, like setting autoresizingMask in -awakeFromNib: I could make the Content View expand to the width of the cell itself, but it was still overlapping to the next cell
UPDATE: It looks like it is not related to the dual table view architecture. When I copy my cell to the tableView where it will show up, has the same issues.
I've found the solution: it was all my fault. In some of the ancestors of my cell has an init, which called -initWithFrame: explicitly with CGRectZero. After removing this, iOS 8 could use it's frame values and everything worked fine.

How does UITableView manage cells with variable height

How does UITableView manages cell with variable height? it asks for height of each and every cell/row on load, but moving further how does it "INTERNALLY" ...
Calculate y origin of each and every cell. Calculating one after another is not an issue.
Decide which cell might be lying in a rect given some arbitrary contentOffset. What and how does it maintain its data structure to perform some fast calculation irrespective of number of rows in table view.
How does it manage addition and deletion of rows and update its data structure without much overhead .
Can someone shed light on this... thanks
Basically you need to be careful using cells with variable height, as it can be a performance problem. Your heightForRowAtIndexPath method can be called for cells that are not currently visible, which can be a very large number in tables with a lot of content. Calculating the origins works just like you imagine it would - it needs to know the heights of all cells above the one it's calculating the origin for.
For cells editing you need to add this
- (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
When you perform the deletion operation it removes the content from the datasource array which you are using in the cellForRowAtIndexPath method .

Trouble with iOS group table

I'm new to iOS programming, and I'm struggling to get a table to look like the one I've designed. I'm using a grouped table, but I can't figure out how to get varying height and how to reduce the padding. Does anyone know a better way to do this than a grouped table? I've attached a link to the image showing what I'm trying to do.
https://docs.google.com/leaf?id=0Bx7qAEhjSDyPYTQyYWUxNjMtMDIxNi00OGM0LTgxODgtMWIxMTcwYmJjNjI3&hl=en_US
That looks like you want to use a one row per section and a different height for each section's row.
The varying height comes from using a different custom cell for each section.
To get individual row height:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
Padding is more complex, you have to override UITableView. See this question.

Resources