2017-08-19 44 views
1

我在我的应用中有一个帐户页面,我希望在桌面视图的第一部分上方显示帐户信息,类似于设置中的Apple ID页面。在UITableView中改变分隔符颜色

AppleIDPage

我的问题是围绕如何构造视图,使得不存在简档图片上面所示隔板。或者让信息看起来在tableview中,但不在一个section中。

从阅读中可以看出,要么显示某些分隔符,要么显示其他分隔符并不可能,要么非常困难。

在苹果ID页面的情况下,图片,名称和电子邮件是在它自己的部分还是属于它下面部分的单元格?

下面的图片是我得到的最接近的,虽然顶部分隔符证明难以隐藏。

MyAttempt

道歉问题的广泛性,但我认为其目的是明确的,一旦达到一个解决方案,我将这个问题以供将来观众更具体。

谢谢。

+0

我追加'UIView'到自定义单元格,将它分离器,隐藏DEFA ult分隔符,所以很容易隐藏/显示/颜色/不管 – JuicyFruit

回答

0

由于开发和PGDev,我能拿出一个很好的解决方案,为static cells运作良好。我为UITableViewCell制作了extension,看起来像这样。

let separatorColour = UIColor(red: 200/255, green: 200/255, blue: 200/255, alpha: 0.5) 

extension UITableViewCell { 
    func separator(topInset: CGFloat, btmInset: CGFloat, showTop: Bool, showBtm: Bool) { 

     if showTop { 
      let topSeparator: UIView = UIView() 
      topSeparator.frame = CGRect(x: topInset, y: 0, width: frame.width, height: 0.5) 
      topSeparator.backgroundColor = separatorColour 
      addSubview(topSeparator) 
     } 

     if showBtm { 
      let btmSeparator: UIView = UIView() 
      btmSeparator.frame = CGRect(x: btmInset, y: frame.height - 0.5, width: frame.width, height: 0.5) 
      btmSeparator.backgroundColor = separatorColour 
      addSubview(btmSeparator) 
     } 
    } 

    func btnSeparator() { 

      let topSeparator: UIView = UIView() 
      topSeparator.frame = CGRect(x: 0, y: 0, width: frame.width, height: 0.5) 
      topSeparator.backgroundColor = separatorColour 
      addSubview(topSeparator) 

      let btmSeparator: UIView = UIView() 
      btmSeparator.frame = CGRect(x: 16, y: frame.height - 0.5, width: frame.width - 32, height: 0.5) 
      btmSeparator.backgroundColor = separatorColour 
      addSubview(btmSeparator) 
    } 
} 

在第一个功能separator()你可以简单地选择顶部和底部separator以及它们是否可见或不可见的插图。

screenshot1screenshot2

TOPCELL 所以在cellname label在图片如上所示,顶部和底部separators的情况下是可见的。但是,因为它应该在该部分中显示为第一个cell,所以它没有顶部插入和适当的底部插入。

MiddleCellcellemail label它只有的情况下separator(带有插图)可见与上述cell具有底部separator

BottomCell 对于cellpassword label,它只具有较低separator(无偏移)可见与上述cell具有底部separator

SpecialCasebtnSeparator()功能只是为了帮助和支持按钮,在这里我想对双方插页的情况。

我们实现这个extension,我用了一个switch statementcellForRowAt函数内部给每个cell适当extension,这是看起来像什么:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = super.tableView(tableView, cellForRowAt: indexPath) 
     let inset: CGFloat = 16   

     switch indexPath { 

     // First Section 
     case [0,1]: 
      cell.separator(topInset: 0, btmInset: inset, showTop: true, showBtm: true) 
     case [0,2]: 
      cell.separator(topInset: inset, btmInset: inset, showTop: false, showBtm: true) 
     case [0,3]: 
      cell.separator(topInset: inset, btmInset: 0, showTop: false, showBtm: true) 

     // Second Section 
     case [1,0]: 
      cell.separator(topInset: 0, btmInset: inset, showTop: true, showBtm: true) 
     case [1,1]: 
      cell.separator(topInset: inset, btmInset: inset, showTop: false, showBtm: true) 
     case [1,2]: 
      cell.separator(topInset: inset, btmInset: 0, showTop: false, showBtm: true) 

     // Third Section 
     case [2,0]: 
      cell.btnSeparator() 
     case [2,1]: 
      cell.separator(topInset: inset, btmInset: 0, showTop: false, showBtm: true) 
     case [2,3]: 
      cell.separator(topInset: 0, btmInset: 0, showTop: true, showBtm: true) 

     default: 
      print("default") 
     } 
     return cell 
    } 

一定要设置separator首屈一指的Storyboard或使用:

tableView.separatorStyle = UITableViewCellStyle.None

1

试试这个

//这将隐藏分隔符的所有行

tableView.separatorStyle = UITableViewCellStyle.None 

//对于自定义分隔符

if indexPath.row != 0{ 
     var aView: UIView = UIView() 
     aView.frame = CGRect(x:0,y:44,width:300,height:1) 
     aView.backgroundColor = UIColor.redColor() 
     cell .addSubview(aView) 
} 
1

您需要在您的UITableViewCell对于使用custom view

设置UITableViewSeparatornonestoryboard

2.创建一个自定义UITableViewCell其中UIView在其中。

class TableCell: UITableViewCell 
{ 
    @IBOutlet weak var separatorView: UIView! 
} 

根据您的要求在cellForRowAt设置的separatorViewbackgroundColor

例子:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
{ 
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableCell 
    switch indexPath.row 
    { 
    case 0: 
     cell.backgroundColor = .red 
    case 1: 
     cell.backgroundColor = .green 
    case 2: 
     cell.backgroundColor = .blue 
    case 3: 
     cell.backgroundColor = .yellow 
    default: 
     cell.backgroundColor = .orange 
    } 
    return cell 
} 

截图:

enter image description here