2017-05-28 23 views
1

我有一個主ViewController是表視圖控制器。我有兩個筆尖文件。其中一個筆尖包含一個表格視圖。我想保留該表格視圖中的數據。我很困惑如何將UItable視圖委託和數據源從nib文件返回到mainView。Swift:返回UItable視圖代表和數據源

這裏是我的代碼:

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 

} 

這是我的主表視圖控制器,我從這裏返回細胞。細胞清晰地顯示在這裏。而ActivitySecondTableViewCell裏面包含Table視圖。

這是我的筆尖:

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細胞的稱號也不是data.How我可以achive他們?

回答

0

請確保您的第一個tableview中有超過2行,然後繼承secondTableView單元格與UITableViewDataSource並給出一些numberOfRows只是爲了檢查並返回cellForRowAt中的UITableViewCell並檢查是否可以看到發生的事情。

+0

我怎麼可以給numberOfRows到secondTableViewCell使用,?? – Sam

+0

在你的ActivitySecondTableViewCell開始輸入numberOfRows,這樣你可以定義numberOfRows。如果建議不會到來,請使用UITableViewDataSource繼承ActivitySecondTableViewCell,然後檢查。 –

+0

你能解決這個問題嗎? –

0

對於不同的筆尖,你可以爲這個

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 


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 
} 
else 
{ 
    let cell : ActivitySecondTableViewCell = (NSBundle.mainBundle().loadNibNamed("IncentiveActivitySecondTableViewCell", owner: self, options: nil).first as? IncentiveActivitySecondTableViewCell)! 
    return cell 
} 
}