Custom separator in tableView
You need to set separator style also not color only. Set separator style
to single line
and make sure that your Separator.jpg
image looks like single line and you are getting color by let color = UIColor(patternImage: UIImage(named:"Separator.jpg")!)
this statement!
you can set seperator style something like,
tableView.separatorStyle = UITableViewCellSeparatorStyle.SingleLine
P.S : If you are checking this on simulator then check it in full resolution (i.e. cmd + 1
or in simulator window - > scale - > 100%
), becasue sometime actually there is no problem but because of smaller scale you can't see it
Update :
Another way : Set SeparatorStyle to None
from interface builder or by code like,
tableView.separatorStyle = UITableViewCellSeparatorStyle.None
then you can add seperator image
(imageview of height 1 pixel or 2 pixel) to cell at bottom in content view of cell by code or interface builder like,
let seperatorImageView = UIImageView.init(image: UIImage.init(named: "yourseperatorimg.jpg"))
seperatorImageView.frame = CGRectMake(0, cell.contentView.frame.size.height - 1.0, cell.contentView.frame.size.width, 1)
cell.contentView.addSubview(seperatorImageView)
Set custom separator insets on UITableViewCell
The best approach to make custom separator is to disable UITableView separator and create a view inside the cell with the height you want such as 1px and then add the constraints to the view to be at center bottom of the cell.
How to customize tableView separator in iPhone
If you want to do more than change the color of the separator using the separatorColor property of the UITableView then you could set the separatorStyle property to UITableViewCellSeparatorStyleNone and then either:
- create custom UITableViewCells that include your custom seperator within them
- create alternate [UITableViewCell][6]s that include your custom separator
For example, if your table currently displays 5 rows you could update it to display 9 rows and the rows at index 1, 3, 5, 7 would be separator cells.
How to add a custom separator to UITableViewCell?
You can add tableView
's standard separator line, and add your custom line at the top of each cell.
In following code Change hight/width/color/image of UIView
for set your separatorLine.
The easiest way to add custom separator is to add simple UIView
of 1px height:
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];
This code might solve your problem :)
Customize separator per cell in UITableView
Your problem is that cell.bounds.size
is not correct when this code is executed, so you can use self.view.bound.size.width
using UIViewController.view
bounds or directly .greatestFiniteMagnitude
I prefer use this last one
Modified Code should be something like this
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = countryCodeView.dequeueReusableCell(withIdentifier: "CountryCell") as! CountryCell
let country = countries[indexPath.row]
cell.flag.text = country.flag
cell.countryCode.text = "+\(country.countryCode)"
cell.countryName.text = country.name
if indexPath.row == 0 {
cell.countryCode.font = UIFont(name: "ProximaNovaA-Bold", size: 16)
cell.countryName.font = UIFont(name: "ProximaNovaA-Bold", size: 16)
cell.separatorInset = UIEdgeInsets(top: 16, left: 16, bottom: 16, right: 16)
cell.tickIcon.isHidden = false
} else {
cell.countryCode.font = UIFont(name: "ProximaNovaA-Regular", size: 16)
cell.countryName.font = UIFont(name: "ProximaNovaA-Regular", size: 16)
cell.separatorInset = UIEdgeInsets(top: 0, left: .greatestFiniteMagnitude, bottom: 0, right: 0)
cell.tickIcon.isHidden = true
}
return cell
}
As alternative you can have an extension for all TableViewCells
extension UITableViewCell {
func hideSeparator() {
self.separatorInset = UIEdgeInsets(top: 0, left: .greatestFiniteMagnitude, bottom: 0, right: 0)
if #available(iOS 11.0, *) {
self.directionalLayoutMargins = .zero
}
}
}
This can be used simply calling this method on cells that you need separator hidden
cell.hideSeparator()
How to set specific UITableViewCell separator position?
i try and it work , in swift
self.tableView.separatorInset = UIEdgeInsets(top: 0, left: 35, bottom: 0, right: 0)
in Objective C:
self.tableView.separatorInset = UIEdgeInsetsMake(0, 80, 0, 0);
How do I add a separator line for a custom UITableView header section nib?
Maybe you need to add a seperator yourself like this :
CGRect seperatorFrame = CGRectMake(0, headerView.frame.size.height-1, tableView.bounds.size.width, 1);
UIView *seperatorView = [[UIView alloc] initWithFrame:seperatorFrame];
seperatorView.backgroundColor = [UIColor grayColor];
[headerView addSubview:seperatorView];
or use Autolayout in .xib:
Create custom UITableView separator line
you can try below:
UIView *separator = [[UIView alloc] initWithFrame:CGRectMake(0, cell.contentView.frame.size.height - 1.0, cell.contentView.frame.size.width, 1)];
separator.backgroundColor = myColor;
[cell.contentView addSubview:separator];
or
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"separator.png"]];
imageView.frame = CGRectMake(0, 100, 320, 1);
[customCell.contentView addSubview:imageView];
return customCell;
}
Related Topics
Ios- Check Whether Current Time Is Between Two Times or Not
How to Properly Add Child View Controller in iOS 8 with Swift
Secure Contents in Documents Directory
Youtube Video Autoplay Inside Uiwebview
Geofire Query on User Location
Measuring Time Accurately in Swift for Comparison Across Devices
Ios-8 and Later - Uitableview Inside an Uialertcontroller
Making Phone Calls on the Iphone
How Can One Enable Keyboard Like in Imessages/Fb Messenger in Landscape Mode at iOS8
Module 'Googlemobileads' Not Found in iOS
Xcode 9 Crash When Open Storyboard
Instantiateviewcontrollerwithidentifier - Storyboard Id Set But Still Not Working
How to Access Extension of Uicolor in Swift
Why Is an Nsdate in a Core Data Managed Object Converted to Nstimeinterval
Property 'Self.*' Not Initialized at Super.Init Call
Supportedinterfaceorientations Not Working