2016-07-27 129 views
0

單擊按鈕我有一個.xib視圖,我在其中爲單元格定義了一個tableView.Here,因爲只有選項,所以我創建了另一個.xib文件。沒有默認的單元格在那裏,我想..所以我怎樣才能顯示在tableview單元格中的項目。 我已經註冊了這個.Xib,並且嘗試了,但我得到了令人驚訝的看法。添加TableView到swift中的.xib視圖

class CustomAddOnVC: UIViewController,UITableViewDataSource,UITableViewDelegate { 

let items = ["pk","ak","sk"] 

override func viewDidLoad() { 
    super.viewDidLoad() 
} 
func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return 1 
} 
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return items.count 
} 
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    let identifier = "Cell" 
    var cell: CustomOneCell! = tableView.dequeueReusableCellWithIdentifier(identifier,forIndexPath: indexPath) as? CustomOneCell 
    if cell == nil { 
     tableView.registerNib(UINib(nibName: "CustomOneCell", bundle: nil), forCellReuseIdentifier: identifier) 
     cell = tableView.dequeueReusableCellWithIdentifier(identifier,forIndexPath:indexPath) as? CustomOneCell 
     cell.addOnName.text = items[indexPath.row] 
     cell.addOnPrice.text = "£0.0" 
    } 

    return cell 
} 

這裏的項目計數爲3,但數據僅填充在第一個。

截圖

enter image description here

還有一件事我如何設置的角度高度,直到所有項目的結束?正如你可以看到項目後的額外空間。如果我減小尺寸和更多的項目來了然後再次同樣problem.I要根據項目數據動態設置高度

回答

1

我認爲這是因爲它不會再輸入您的cell == nil當你第一次註冊。因此,我的建議是在if語句之外移動更新文本。就像這樣:

if cell == nil { 
    tableView.registerNib(UINib(nibName: "CustomOneCell", bundle: nil), forCellReuseIdentifier: identifier) 
    cell = tableView.dequeueReusableCellWithIdentifier(identifier,forIndexPath:indexPath) as? CustomOneCell 
} 
cell.addOnName.text = items[indexPath.row] 
cell.addOnPrice.text = "£0.0"` 
+0

Ø是啊..感謝它的工作.. –

+0

但如何根據項目我設置視圖大小...例如,如果我只有一個數據,那麼該視圖的大小應表格的大小 –

+0

如果您使用自動佈局,則需要更新約束。也許創建一個新的問題,並顯示一些代碼,你會嘗試或面臨困難,所以它會更清晰。 –

相關問題