所以,這是我的問題:iOS Swift - 是否有將子視圖添加到表格單元格並將其正確定位的方法?
我想創建一個表來顯示一些航班數據。這個想法是,每個單元格代表一次旅行(即從城市A到B所需的所有航班)。每個航班有3個屬性,我將分成不同的標籤,組成線路。因此,細胞必須相應地調整其高度。
這是I`ve迄今所做的:
我的視圖控制器類:
@IBOutlet weak var dispTable: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
dispTable.dataSource = self
dispTable.delegate = self
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCellWithIdentifier("DispCell") as? DispTableViewCell {
let flight1: Flight = Flight()
let flight2: Flight = Flight()
curFlightsIda = [Flight]()
Flight1.Num = "1221"
Flight2.Num = "1331"
curFlightsIda.append(Flight1)
curFlightsIda.append(Flight2)
for voo in curFlightsIda {
let lbl = UILabel(frame: CGRectMake(0, 0, 100, 50))
lbl.textAlignment = NSTextAlignment.Center
lbl.text = voo.Num
cell.contentView.addSubview(lbl)
}
return cell
} else {
return DispTableViewCell()
}
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 100
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
的flight.num存在只是爲了測試。我能夠以編程方式創建標籤,但我覺得我的方式正常工作。m going, the layout won
那麼,我該如何創建代表行的標籤並將它們正確放置?我在正確的軌道上嗎?我搜索了四周,發現了XIB,但我不確定它是否更好..?老實說,我完全失去了,任何幫助將深表謝意。
我真的不知道這會有所幫助,但它會是這樣的:
TRIP CELL:
Flight1Num Flight1Departure Flight1Duration
Flight2Num Flight2Departure Flight2Duration
Flight3Num Flight3Departure Flight3Duration
您應該使用自動佈局。 – dasdom