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);
}
Related
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
I want to add a cell in the bottom that shows only if the app is loading more results when the user scrolls to the bottom of a tableview. I already did that. The problem is that it's a cell. So when I scroll down even further, the separator shows that it's just another cell in the table. I want to hide that. I have to change that cell's separator color, how do I do that to a particular cell? Also, Where/when do i do that?
I realize that if I change that cell's color (let's say it's #5), when I get the results and load them in, won't that also result in cell #5 still having an invisible separator? So how do I set it back to the default color? More particularly how do I get the default color for a tableviewcellseparator programmatically?
Thanks
Here's a simple trick: you have to add en empty UIView inside your UITableView in Storyboard. The set the height = 0.
The last separator of the UITableView magically disappears.
Edit:
You will also need to edit your cellForRowAtIndexPath: like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"identifier"];
if (indexPath.row == [self tableView:tableView numberOfRowsInSection:[self numberOfSectionsInTableView:tableView]-1]-1) {
//this is the last cell
cell.separatorInset = UIEdgeInsetsMake(0, cell.bounds.size.width, 0, 0);
} else {
cell.separatorInset = UIEdgeInsetsMake(0, 0, cell.bounds.size.width, 0);
}
return cell;
}
Separator colour is a property of the table view, you can get or set it like so:
self.tableView.separatorColor = UIColor.blackColor()
But I don't think you cannot change the separator colours of a individual cell.
If you truly need that level of customization, you should probably just draw the separator bar yourself as, for example, a thin UIView at the top of the cell. Then you can change its appearance to whatever you want.
Hi I'm new to ios development. I am developing an application in which I am using UITableView.
Tableview contains some cells with separator line and others without separator line.
Can any one please tell me how can I remove separator line for specific cell?
cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, cell.bounds.size.width);
Above code works for iOS 7. How can I achieve this in iOS6?
Any help is appreciated
You can just add a UIView with the same background color as you want your separator line to be at the top of your UITableViewCell layout, make its height one pixel (it will be the same as a line) and its width the same as your cell width (cell.frame.size.width). After this in your cellForRowAtIndexPath just set hidden property of this to YES for the cell you don't want it to be shown and hidden = NO for those you want. It is a simple solution which will work in any iOS version. But if will add your custom separator don't forget to disable default separators for all tableview
In order to have only specific cells with separator you have to draw separator lines for those cells. In my view set the separator style to none and then draw you custom separator for the desired cells.
You can follow this link for custom separators
Basically what u need to do is add a custom view as follows:
UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 1)];/// change size as you need.
separatorLineView.backgroundColor = [UIColor grayColor];// you can also put image here
[cell.contentView addSubview:separatorLineView];
to set the separator style to none use:
tableView.separatorStyle=UITableViewCellSeparatorStyleNone
If you try to change the color of the separatorLine for this. But one thing is that if you want to make the adjacent cells as a single cell then this would not be the right approach. As this will make only the UI change.
The better approach would be to adjust the size of the cell and so that you get "single cellClick events for them".
you can use the delegate for this:-
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
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];