我有一个主视图 Controller ,它是 TableView Controller 。我有两个 nib 文件。其中一个 Nib 包含一个 TableView 。我想将数据保存在该 TableView 中。我很困惑如何将 UItable View 委托(delegate)和数据源从 nib 文件返回到 mainView。
这是我的代码:
var participationList : ["Apple","Dog","cat","Mat"]
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if indexPath.row == 0
{
//first cell
}
if indexPath.row == 1
{
let cell : ActivitySecondTableViewCell = (NSBundle.mainBundle().loadNibNamed("ActivitySecondTableViewCell", owner: self, options: nil).first as? ActivitySecondTableViewCell)!
cell.textLabel?.text = self.participationList[indexPath.row]
return cell
}
let cell : ActivitySecondTableViewCell = (NSBundle.mainBundle().loadNibNamed("IncentiveActivitySecondTableViewCell", owner: self, options: nil).first as? IncentiveActivitySecondTableViewCell)!
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 2
}
这是我的主 TableView Controller ,我从这里返回单元格。细胞在这里清楚地显示。 ActivitySecondTableViewCell 包含其中的 TableView 。
这是我的 Nib :
class ActivitySecondTableViewCell: UITableViewCell,UITableViewDelegate {
@IBOutlet weak var CellTableView: UITableView!
override func awakeFromNib() {
super.awakeFromNib()
self.CellTableView.delegate = self
// Initialization code
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "Required Health Activities for 2017"
}
func tableView(tableView: UITableView, willDisplayHeaderView view:UIView, forSection: Int) {
if let headerTitle = view as? UITableViewHeaderFooterView {
headerTitle.textLabel?.textColor = UIColor.redColor()
}
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
我既看不到 Activitysecond 单元格中的标题,也看不到数据。我怎样才能获得它们??
最佳答案
确保您的第一个 tableview 中有超过 2 行,然后使用 UITableViewDataSource 继承 secondTableView 单元格并提供一些 numberOfRows 只是为了检查并在 cellForRowAt 中返回 UITableViewCell 并检查您是否可以看到正在发生的事情。
关于ios - Swift:返回 UItable View 委托(delegate)和数据源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44223543/