2016-07-27 149 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

如果您使用自动布局,则需要更新约束。也许创建一个新的问题,并显示一些代码,你会尝试或面临困难,所以它会更清晰。 –

相关问题