Swift: How to create space around cells in a static UITableView - ios

I am trying to create a gap between my cells in a static UITableView. The UITableView has 3 sections, each containing 2 rows. I have tried
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 15
}
but this method does not seem to work.

This might be a little bit of a hack but it has worked for me in the past.
Simply add a UIView to the UITableViewCell and set it as a different background colour to the UITableView.
If you make the UIView smaller than the UITableViewCell, you will be able to get the desired spacing just by adding constraints.
I hope I've understood the correctly

This isn't something that UITableView does by default. There isn't a simple way to add spacing between cells, that's generally something you'd accomplish by using a UICollectionView.
If you have to use a table view you can try adding the padding into a custom UITableViewCell itself, which will create the affect you're looking for. You could also try adding additional "spacer" cells that go in between your primary content cells.

Related

UITableViews inside UIScrollView

I have read a few posts asking about to put UITableViews inside a ScrollView but mostly this idea is not recommended. The "standard way" to handle this situation as I understand is to use just the UITableView instead of the ScrollView and make the TableView header the UIView to display the content and to use the cell to display any dynamic rows.
However the app I am now developing is a bit different: the first part of the view is some descriptive content that I can use the header view to display. But after that I want to display two scrollable sections potentially with dynamic height. One is a comment section displaying all comments and replies and another is a user section that display any users ever made an offer. This two sections need to be row-based and will be dynamic in height as the content inside them will be different.
So the question is, how should I implement this without putting two UITableViews inside a UIScrollView(My original plan)?
UITableViews inside a ScrollView is not at all recommended as UITableView as its own scrollView as subView.
This two sections need to be row-based and will be dynamic in height
as the content inside them will be different.
Whats wrong with using the delegate for dynamic height for row
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {}
Some Tutorial:
https://www.raywenderlich.com/129059/self-sizing-table-view-cells

Insets around section of UITableView

Is there a way to give spacing around a section? I know this can be done using UICollectionView. Have implemented all the things using UITableView and UITableViewCells, don't want to move things to UICollectionView.
I am trying to achieve space around the section(all sides) to give a group kind of feel for section(including header, cells and footer views).
You can use tableView(_ tableView: UITableView, viewForFooterInSection section: Int) and tableView(_ tableView: UITableView, heightForFooterInSection section: Int) to add more space between sections without messing with margins.
There is no straight forward api for this.
Add some space on every side of UI elements which you have added on top of section header view. eg. (10,10,width,height)
Apply same gap for Cell subviews as well.
If you created your table view section header via IB, you should add the one more UIView object before doing anything else. Then, you have to add correct constraints to this view relative to the header content view. I think you will add your margins from each side. Then you should adjust your table view background colour. Now I think you are free to make something like you described
You could actually use the tableView separator inset, with the separator style of none, and That's it!
I know it's a bit late but hopefully the answer could be of use for others.

Prevent UITableViewCell from scrolling under viewForHeaderInSection

I have a fairly complicated UIViewController with a custom UITableView instantiating on top of a Visual Effect & UIImageView. In order to keep the beautiful blur effect throughout the UITableView, I had to set the entire thing's background color (including UITableViewCells and viewForHeaderInSection) to UIColor.clearColor(). The obvious problem (that I can't solve) is that when I scroll up, the text of the UITableViewCell will scroll under the clear background of the viewForHeaderInSection and both text will overlap. Here's a screenshot.
Any thoughts on how I can prevent the UITableViewCell from scrolling under the viewForHeaderInSection? I was playing around with the UITableView's contentInsets but that changes the entire UITableView, not just the UITableViewCell sections. Thx in advance...
1) Select the tableview in storyboard.
2) Select attribute inspector.
3) Change the style from plain to grouped.

How to correctly modify iOS table view cells at runtime (add/remove subviews?)

Apologies in advance if this has been answered somewhere. I've looked everywhere and I'm still unsure what to do. (And answers that use Objective C are almost completely worthless to me.) I'm somewhat new to iOS.
I have a UITableView which serves as a newsfeed of sorts, displaying a series of posts. (a la a twitter newsfeed, for instance.) The cells in this table view are (currently) derived from a single cell prototype, created in xcode's interface builder. Each cell contains subviews to display things like username, profile image thumbnail, title, message, date, location, another image, etc.
The problem is, depending on what data a particular post contains, many of these subviews either should or should not be shown -- if a post doesn't contain an image, then that cell's image view should not be shown; if a post doesn't have a date and/or location, one or both of these views should not be shown. Not only should the unused fields be empty, but they shouldn't take up any space in the cell.
I read in Using Auto Layout in UITableView for dynamic cell layouts & variable row heights (under "2. Determine Unique Table View Cell Reuse Identifiers". Wonderful answer by #smileyborg, btw.) that for each different layout of subviews that could be in a cell, a different prototype cell and reuse identifier should be used. But this would require me to have a cell prototype for every single possible combination of data items in a post, even if the difference is single label! Surely there must be a better way.
What is the safe and correct way to do what I need to do? Is there perhaps a way to remove subviews from cells at runtime (and have the layout adjust its spacing accordingly) without completely screwing up cell recycling?
Ill assume you know everything you want to know about your layout when you see the cell.
So its basic tableview cell layout. Dequeue and decorate.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(MyIdentifier, forIndexPath: indexPath) as! MyCustomCellClass
let data:MyDataClass = myDataAtIndexPath(indexPath)
decorateCell(cell,data:data)
return cell
}
func decorateCell(cell:MyCustomCellClass,data:MyDataClass) {
//here is where you arrange/change your constraints and hide/reveal views
//depending on the data
}
but also what you need is to allow the cell to set its height properly
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return someEstimatedNumberWhichIsClose;
}
but most crucially in your XIB, the cell MUST have a continuous line of constraints from top to bottom which describe the height.
So this cell will auto-set its height correctly.
While this one will not.
NB - in the upper example , if you leave the height constraint off the text-field and make it infinite line count the cell will grow/shrink appropriately.
So to summarise you can use one multipurpose cell as long as you keep your height constraints coherent.
As a general rule - Swiss Army CellsĀ® which do lots of things may get a bit unwieldy so you do need think about how many use cases you wish to support with one cell and then maybe start creating multiple types/identifiers.

How to set Frame properly in Viewdidappear using reference of cell

I want to display captured video using MPMoviePlayer for that purpose I designed one customcell and number of sections is one and I am loading ten by ten records in tableView so number of rows depends on array count so every time ten by ten records displaying from getService and my problem is frame is not setting properly in cell for that purpose I wrote entire code in one method and called that method in viewDidAppear() and when getting cell reference in viewDidAppear() getting fatal error in NSIndexPath I am thinking that viewDidAppear() is calling before I am getting cell reference so how to solve this problem please tell me any solution?
You are not supposed to manipulate with frames of cells from viewDidAppear of your view controller.
A frame consists of: left, top, width, and height.
left is always 0;
top is specified by a table and is based on the index path and heights of previous cells, section headers, and a table's header;
width is always equal to a width of the table;
height you can specify by yourself by overriding UITableViewDelegate's method optional func tableView(_ tableView: UITableView,
heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
But most likely you are interested in a different thing. And you want to layout UI elements within you cells. If yes, you have a few ways to do this:
Override func layoutSubviews() method in the cell and set all frames you want. This method is called every time when frame is changed. So it's exactly what you need.
Also you can specify the desired layout using auto layouts. This may be done in a storyboard/xib file or in code (in a designated constructor of the cell).

Resources