我有一个应用程序,其中UITableView的分隔符插入设置为自定义值-右0,左0。这在iOS 7中非常有效。x,但是在iOS 8.0中,我看到右边的分隔符被设置为默认值15。即使在xib文件中它设置为0,它仍然不正确地显示。
如何删除UITableViewCell分隔符边距?
我有一个应用程序,其中UITableView的分隔符插入设置为自定义值-右0,左0。这在iOS 7中非常有效。x,但是在iOS 8.0中,我看到右边的分隔符被设置为默认值15。即使在xib文件中它设置为0,它仍然不正确地显示。
如何删除UITableViewCell分隔符边距?
当前回答
对我来说,除了这个解决方案(Swift 3.0)之外,其他方法都不起作用:
extension UIColor {
static func colorWith(hex:String, alpha: CGFloat) -> UIColor {
var cString = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()
if cString.hasPrefix("#") {
cString.remove(at: cString.startIndex)
}
if cString.characters.count != 6 {
return UIColor.gray
}
var rgbValue:UInt32 = 0
Scanner(string: cString).scanHexInt32(&rgbValue)
return UIColor(red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
blue: CGFloat( rgbValue & 0x0000FF) / 255.0,
alpha: alpha)
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellReuseIdendifier, for: indexPath)
cell.backgroundColor = UIColor.colorWith(hex: "c8c7cc", alpha: 1) // same color of line separator
return cell
}
其他回答
Swift 3.0示例:
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
// removing seperator inset
if cell.responds(to: #selector(setter: UITableViewCell.separatorInset)) {
cell.separatorInset = .zero
}
// prevent the cell from inheriting the tableView's margin settings
if cell.responds(to: #selector(setter: UIView.preservesSuperviewLayoutMargins)) {
cell.preservesSuperviewLayoutMargins = false
}
// explicitly setting cell's layout margins
if cell.responds(to: #selector(setter: UITableViewCell.layoutMargins)) {
cell.layoutMargins = .zero
}
}
Swift 2.0扩展
我只是想分享一个扩展,我做了删除边缘从tableview单元格分隔符。
extension UITableViewCell {
func removeMargins() {
if self.respondsToSelector("setSeparatorInset:") {
self.separatorInset = UIEdgeInsetsZero
}
if self.respondsToSelector("setPreservesSuperviewLayoutMargins:") {
self.preservesSuperviewLayoutMargins = false
}
if self.respondsToSelector("setLayoutMargins:") {
self.layoutMargins = UIEdgeInsetsZero
}
}
}
用于上下文中:
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CustomCell
cell.removeMargins()
return cell
只需重写cellForRowAtIndexPath并设置cell。preservessuperviewlayoutmargin = false" and "cell。separatorInset = UIEdgeInsets。零”和“单元格”。layoutmargin = uiedgeinsets . 0 "
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: LayoutTableViewCell! = tableView.dequeueReusableCell(withIdentifier: "LayoutCell") as? LayoutTableViewCell
let currentLayout = OrderLayouts()[indexPath.row]
cell.NameLabel?.text = currentLayout.name
cell.DescrLabel?.text = currentLayout.descr
if(GlobalVariables.debug){
cell.DescrLabel?.text = "\(currentLayout.id) \n \(currentLayout.descr)"
}
cell.preservesSuperviewLayoutMargins = false
cell.separatorInset = UIEdgeInsets.zero
cell.layoutMargins = UIEdgeInsets.zero
return cell
}
这是在Swift中为我工作的代码:
override func viewDidLoad()
{
super.viewDidLoad()
...
if tableView.respondsToSelector("setSeparatorInset:") {
tableView.separatorInset = UIEdgeInsetsZero
}
}
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell,forRowAtIndexPath indexPath: NSIndexPath)
{
if cell.respondsToSelector("setSeparatorInset:") {
cell.separatorInset.left = CGFloat(0.0)
}
if tableView.respondsToSelector("setLayoutMargins:") {
tableView.layoutMargins = UIEdgeInsetsZero
}
if cell.respondsToSelector("setLayoutMargins:") {
cell.layoutMargins.left = CGFloat(0.0)
}
}
这对我来说似乎是最干净的(目前),因为所有的单元格/tableView边缘/边缘调整都是在tableView:willDisplayCell:forRowAtIndexPath:方法中完成的,而不是在tableView:cellForRowAtIndexPath:中填充不必要的代码。
顺便说一句,我只是设置单元格的左separatorInset/ layoutmargin,因为在这种情况下,我不想搞砸我在单元格中设置的约束。
代码更新到Swift 2.2:
override func viewDidLoad() {
super.viewDidLoad()
if tableView.respondsToSelector(Selector("setSeparatorInset:")) {
tableView.separatorInset = UIEdgeInsetsZero
}
}
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell,forRowAtIndexPath indexPath: NSIndexPath) {
if cell.respondsToSelector(Selector("setSeparatorInset:")) {
cell.separatorInset.left = CGFloat(0.0)
}
if tableView.respondsToSelector(Selector("setLayoutMargins:")) {
tableView.layoutMargins = UIEdgeInsetsZero
}
if cell.respondsToSelector(Selector("setLayoutMargins:")) {
cell.layoutMargins.left = CGFloat(0.0)
}
}
使用Swift 2.2
创建UITableViewCell扩展
import UIKit
extension UITableViewCell {
func removeMargins() {
if self.respondsToSelector(Selector("setSeparatorInset:")) {
self.separatorInset = UIEdgeInsetsZero
}
if self.respondsToSelector(Selector("setPreservesSuperviewLayoutMargins:")) {
self.preservesSuperviewLayoutMargins = false
}
if self.respondsToSelector(Selector("setLayoutMargins:")) {
self.layoutMargins = UIEdgeInsetsZero
}
}
}
现在可以在cellForRowAtIndex中使用
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.removeMargins()//To remove seprator inset
}