2016-07-25 50 views
1

這就是我想要實現的。Swift - 如何製作包含標籤的表格

enter image description here

我試圖編碼它自己,但第一外視圖邊框沒有顯示出來。

這裏是我的代碼:

import UIKit 

class InfoTableView: UIView { 

    override func drawRect(rect: CGRect) { 

     self.backgroundColor = UIColor.whiteColor() 

     let outerBorder = UIColorCode.init(hexString: "#666666") 
    let startingTopPoint = CGPoint(x: rect.minX, y: rect.minY) 
    let endingTopPoint = CGPoint(x: rect.maxX, y: rect.minY) 

    let startingPoint = CGPoint(x: rect.minX, y: rect.maxY) 
    let endingPoint  = CGPoint(x: rect.maxX, y: rect.maxY) 

     // top 
     let tpPath = UIBezierPath() 
     tpPath.moveToPoint(startingPoint) 
     tpPath.addLineToPoint(endingTopPoint) 
     tpPath.lineWidth = 2.0 

     outerBorder.setStroke() 
     tpPath.stroke() 

     // bottom 
     let btPath = UIBezierPath() 

     btPath.moveToPoint(startingPoint) 
     btPath.addLineToPoint(endingPoint) 
     btPath.lineWidth = 2.0 

     outerBorder.setStroke() 
     btPath.stroke() 

    } 
} 

有頂部外邊框和底部。但只有底部的一個出現。我不知道我哪裏錯了。

+0

喜約翰,嘗試一下在UITableView的,它是可定製的,更容易有動態數據 – xmhafiz

+1

@約翰而是另起爐竈,我的事情你應該給一個嘗試的UITableView並嘗試自定義的UITableViewCell。您可以通過少量定製和少量編碼來獲得輸出結果。 –

回答

0

我已經對您的代碼進行了一些編輯。試試它是否適合你。

override func drawRect(rect: CGRect) { 
     // Drawing code 
     self.backgroundColor = UIColor.whiteColor() 
     let outerBorder = UIColor.redColor() 
     let lineWidth : CGFloat = 2.0 
     let insetRect = rect.insetBy(dx: lineWidth/2, dy: lineWidth/2) 
     let startingTopPoint = CGPointMake(insetRect.origin.x,insetRect.origin.y) 
     let endingTopPoint = CGPoint(x: insetRect.maxX, y: insetRect.minY) 

     let startingPoint = CGPoint(x: insetRect.minX, y: insetRect.maxY) 
     let endingPoint  = CGPoint(x: insetRect.maxX, y: insetRect.maxY) 

     // top 
     let tpPath = UIBezierPath() 
     tpPath.moveToPoint(startingTopPoint) 
     tpPath.addLineToPoint(endingTopPoint) 
     tpPath.lineWidth = 2.0 

     outerBorder.setStroke() 
     tpPath.stroke() 

     // bottom 
     let btPath = UIBezierPath() 

     btPath.moveToPoint(startingPoint) 
     btPath.addLineToPoint(endingPoint) 
     btPath.lineWidth = 2.0 

     outerBorder.setStroke() 
     btPath.stroke() 
    }