I am trying to delete a single separator in a tableView. I have annotated the screenshot below to show this. I am wanting to delete the first cells top separator. My top text & search bar is in the tableView's header. How would I achieve this?
Please try with this code
Original answer for this code is #Avinash
For mode detail please check avinash answer
if indexPath.row == {your row number} {
cell.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: .greatestFiniteMagnitude)
}
Add a separator view (UIView) as part of your Custom cell. From the nib make the separator for UITableView to none.
tblView.separatorStyle = .none
Related
I have a UITableView where the last cell is cut off behind the UITabBarController. I set the bottom constraint of the tableView to the top of my bottomLayoutGuide. I also tried this approach without success.
The only solution that worked was adding insets to the content (one point was enough):
tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 1, right: 0)
Therefore I wonder if that's a normal behaviour or a scroll view bug.
How to change spacing between the navigation bar and the first collectionViewCell?
I want to be aligned in the middle. In this case, the only way to calculate the screen size?
If you want to add space at the top of your collection view you can use contentInset. There are probably other ways to meet your requirement but this is the easiest way I can think of.
collectionView.contentInset = UIEdgeInsets(top: 100, left: 0, bottom: 0, right: 0)
I don't understand what you are trying to do. Can you explain more?
What I'd like to do is remove the separator for a single cell in a tableview. I'd like to keep the others ones as they are but I can't seem to find a way to do it.
I thought I could adjust the insets but from what I can see, these aren't the insets that I am trying to modify.
One easy way to get the same effect, but not answering exactly your question, is to disable UITableView's separators and adding a 1px-height UIView to the prototype cells you want to have it.
Yes, you can set cell separator inset in willDisplayCell.
if (indexPath.row == HideTheRowSeparator) {
cell.separatorInset = UIEdgeInsetsMake(0, tableView.bounds.size.width, 0, 0);
}
I require a UITableViewCell separator for the majority of my cells, but some of them should be hidden.
The way this is set is self.tableView.separatorStyle, which would apply to every cell.
A heavy-handed work around is to disable it, and then draw it onto cells manually as a UIView, but I don't want to go there.
How can I otherwise remove the separator for individual cells?
You can have custom cell and add separator in this cell, this way you can manage your cell separator. Or you can addSubView a UIView for separator to your cell like iPatel's answer, but be careful for memory issue.
Use a different ReUseIdentifier for the cells which you don need separatorStyle.Don draw anything onto this cell.
Adding a custom view as a separator is a lot of work. Besides adding a property, setting constraints, etc, you need to worry about matching the color and line width of the default separators (1.0 / view.window.screen.scale so it's one pixel tall, not one point).
It's way easier to use the default separator view, and hide it by setting the insets. For example, turn on separators for the table, then in your UITableViewCell subclass do something like this:
var showsSeperator: Bool = true {
didSet {
let leftInset: CGFloat
if showsSeperator {
leftInset = layoutMargins.left
} else {
leftInset = 1000 // Out of site.
}
separatorInset = UIEdgeInsets(top: 0, left: leftInset, bottom: 0, right: 0)
}
}
I need to add some blank space to the top of my UITableView that does not affect the size of the content area. Shifting the content down or adding a blank cell is NOT what I want to do. Instead I just want an offset.
How?
I'm not sure if I'm following you but I think I'm having the same predicament. In my case I must give some space to the ADBannerView at the top of the screen so what I did was in the viewDidLoad method I added:
[self.tableView setContentInset:UIEdgeInsetsMake(50,0,0,0)];
the values it takes are UIEdgeInsetsMake(top,left,bottom,right).
Alternatively the same with Swift:
self.tableView.contentInset = UIEdgeInsetsMake(50, 0, 0, 0)
Swift 4.2:
self.tableView.contentInset = UIEdgeInsets(top: 50, left: 0, bottom: 0, right: 0)
Swift 5.1
add the following in viewDidLoad
tableView.contentInset.top = 100
Really that's all there is to it.
override func viewDidLoad() {
super.viewDidLoad()
tableView.contentInset.top = 100
}
You can add an "empty" header view to the table... this would give the initial appearance of the table to have an offset, but once you started scrolling the offset would be gone. NOt sure that's what you want.
If you need a permanent offset and are not already using section headers, then you could create the offset similarly to above by making custom views for the section headers, especially if you just have one section, this could give you the look of a permanent offset.
I can post sample code if it sounds like either of those are what you are looking for.
I combined Jigzat's answer with:
[self.tableView scrollRectToVisible:CGRectMake(0, 0, 320, 1) animated:NO];
in
- (void)viewDidLoad
so the first cell isn't at the top.
Sounds like you want to wrap a 'View' around your UITableView. If you have a UITableViewController in IB the UITableView will automatically be set to the view of UITableViewController. You change view property to a normal UIView and add your UITableView in there and give it a offset.
---Edit---
I just read my post and thought it made little sense :) When you create a UITableViewController you get this (in pseudo code):
UITableViewController.view = UITableView
This means that the actual table will take up the whole space and you cannot even add other views. So you need to change the
UITableViewController.view = UIView
and add your table to that UIView
I combined this answer with this one:
https://stackoverflow.com/a/9450345/1993937
To make the tableView appear at the top of the content inset, so the space at the top isn't cut off by having the tableView scrolled down slightly when the view initially appears. (18 is my top gap)
[self.tableView setContentInset:UIEdgeInsetsMake(18,0,0,0)];
[self.tableView setContentOffset:
CGPointMake(0, -self.songListTable.contentInset.top) animated:YES];