2017-03-16 64 views
1

我有一個tableview以編程方式添加下面,我想掛鉤委託和數據源到外部類。代碼看起來正確,但是tableview被添加到視圖中,而不從外部類獲取單元格佈局。TableView外部數據源和委託沒有加載

let tableView: UITableView = { 
    let dataService = ActivityDataService() 
    let tb = UITableView() 
    tb.tableHeaderView = nil 
    tb.tableFooterView = nil 
    tb.rowHeight = 50 
    tb.estimatedRowHeight = 50 
    tb.dataSource = dataService 
    tb.delegate = dataService 
    tb.register(ProfileActivitySubCell.self, forCellReuseIdentifier: "tableCell") 
    return tb 
}() 

下面是活動服務類:

class ActivityDataService: NSObject, UITableViewDelegate, UITableViewDataSource { 

func numberOfSections(in tableView: UITableView) -> Int { 
    return 1 
} 
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 4 
} 
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "tableCell", for: indexPath) as! ProfileActivitySubCell 
    return cell 
} 

func tableView(_ tableView: UITableView, estimatedHeightForHeaderInSection section: Int) -> CGFloat { 
    return 0 
} 

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat { 
    return 50 
} 

} 

感謝

+0

什麼是信息? –

+0

用於單元格,標籤等的UI – user7684436

+0

標籤,UI在哪裏?你在哪裏添加了你的信息? –

回答

0

當使用UITableView那不是在故事板或類似的,你需要將細胞與標識登記。

根據您的UITableViewCell(如果它的一個子類和/或如果您正在使用筆尖或不)

你可以使用以下方法之一:

open func register(_ nib: UINib?, forCellReuseIdentifier identifier: String) 

open func register(_ cellClass: Swift.AnyClass?, forCellReuseIdentifier identifier: String) 

這是UITableView

方法

在你的情況大概是這樣的:

tb.register(ProfileActivitySubCell.classForCoder(), forCellReuseIdentifier: "tableCell")

+0

tableview在一個集合視圖單元格內。我可以與表格視圖交互(反彈)。 – user7684436

+0

嘗試了這種方法,但沒有奏效。 – user7684436

0

1)重構表視圖數據源的方法來單獨的類

class IceCreamListDataSource: NSObject, UITableViewDataSource 
    { 


     // MARK: - Table view data source 

     func numberOfSectionsInTableView(tableView: UITableView) -> Int 
     { 
     return 1 
     } 

     func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
     { 
     return 5 
     } 

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

     let cell = tableView.dequeueReusableCellWithIdentifier("IceCreamListCell", forIndexPath: indexPath) 

     return cell 
     } 
    } 

2)在您的控制器類做這個 - :

class IceCreamListViewController: UITableViewController 
{ 
    let dataSource = IceCreamListDataSource() 

    // MARK: - View lifecycle 

    override func viewDidLoad() 
    { 
    super.viewDidLoad() 
    tableView.dataSource = dataSource 
    } 
} 
+0

試過這個,它沒有工作。 – user7684436

+0

@ user7684436你可以顯示你的代碼。 –

+0

當然:讓dataService = ActivityDataService() \t \t \t let cell = collectionView.dequeueReusableCell(withReuseIdentifier:activityCell,for:indexPath)as! ProfileActivityCell \t \t \t cell.tableView.delegate = DataService的 \t \t \t cell.tableView.dataSource = DataService的 \t \t \t返回細胞 – user7684436

0

我設法解決這個問題。由於tableView在一個集合視圖中,我不得不使用一個storyboard對象插座。最後在集合視圖單元格內,我必須將委託和數據源設置爲新創建的對象。

cell.tableView.dataSource = dataService 
cell.tableView.delegate = dataService 
相關問題