2016-11-03 78 views
-1

我一直在尋找一段時間沒有運氣。我試圖找到一個具有UITableView的View Controller的示例。我看到的例子都是處理Table View Controller,我不能使用它,因爲我需要控制表視圖內容的相同視圖中的按鈕。任何人都有一個例子,知道一個例子或有想實現這樣的想法?謝謝。Swift視圖控制器與UITableView部分

編輯

我有一個表視圖在一個視圖控制器,得到API調用的數據,在一個結構數組的部分和數據分開。然後我發送這個綁定到表視圖。這樣做會引發

[UIView的的tableView:numberOfRowsInSection:]:無法識別的選擇發送到實例

,但我不明白問題出在哪裏。

代碼爲tablview

//MARK: Tableview delegates 
func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    if let count = incidentDataSection?.count{ 
     return count 
    } 
    return 0 
} 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    if (incidentDataSection?.count)! > 0{ 
     return incidentDataSection![section].incidents.count 
    } 
    return 0 
} 

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
    return incidentDataSection?[section].title 
} 
/* 
func tableView(tableView: UITableView, iconForHeaderInSection section: Int) -> UIImage? { 
    return incidentDataSection?[section].icon 
}*/ 

//if clicked, will openn details view passing in the details 
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    //let incidentDetails = incidentData?[indexPath.row] 

} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    if let section = incidentDataSection?[indexPath.section] { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "IncidentTableViewCell") as! IncidentTableViewCell 
     cell.roadNameLabel.text = section.incidents[indexPath.row].RoadWay 
     cell.whenLabel.text = section.incidents[indexPath.row].DateCreated 
     cell.statusLabel.text = section.incidents[indexPath.row].DateCleared 
     return cell 
    } 
    return UITableViewCell() 
} 

incidentDataSection是具有部分標題和不同項的結構的陣列。

回答

雖然我收到了一些相當不錯的反饋,原因竟是一個錯字。仔細看看

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
    return incidentDataSection?[section].title 
} 

你會注意到問題是在tableView:之前沒有下劃線。發生了什麼事情是數據源和代表跳過了函數,因爲有和沒有調用不同的協議在迅速3.感謝這個link我能夠找出原因。我忘記提到這件事的糟糕之處在於Swift 3.可能會挽救所有人一段時間。

+0

你知道如何添加一個表視圖到視圖控制器嗎?一旦你這樣做了,無論它是視圖控制器還是表視圖控制器,它都具有相同的部分。 – rmaddy

+0

是的,當它只是一個普通的列表時,我沒有執行表視圖的問題,但是我正遇到一個挑戰,使它能夠與分區一起工作。 –

+0

然後請更新您的問題與您嘗試過的內容,並清楚說明您嘗試讓部分工作的問題。 – rmaddy

回答

0

在視圖控制器中需要一個tableview實例。 作爲UITableViewController在您的視圖控制器中實現協議UITableViewDelegate,UITableViewDataSource。

不要忘記將XIB中的tableview與tableview類綁定在一起。

看看這個例子:

class Sample01ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 
    var tableView: UITableView? 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     tableView?.delegate = self 
    } 

    override func viewDidAppear(_ animated: Bool) { 
     super.viewDidAppear(true) 

     self.tableView?.reloadData() 
    } 

    // ... 
+0

這並沒有真正回答這個問題。問題更多的是讓部分在表視圖中工作,而不是將表視圖添加到視圖控制器。而你的答案缺少許多關於讓表視圖實際工作的相關細節。你忘了設置表視圖的'dataSource'。 – rmaddy

0

你所需要的方法來實現,但它聽起來像你需要「繼承」或「SUBCRIBE」到的UITableView的delegatedataSource。通過使用:

class MyViewController : UIViewController, UITableViewDelegate, UITableViewDataSource { 
    @IBOutlet var tableView : UITableView! 
} 

現在,你擁有了這些,你需要將你的tableView的委託和數據源設置爲您的viewController協議。您可以使用storyboard通過拖放操作來執行此操作,也可以在您的viewDidLoad()內部執行此操作,這是我始終執行的操作,因爲其他開發人員可以輕鬆地從代碼和數據源分配到的位置打開代碼。使用:

@override func viewDidLoad() { 
    super.viewDidLoad() 
    tableView.delegate = self 
    tableView.dataSource = self 
} 

然後您的委託方法和您的viewcontroller中的dataSource方法將被調用該tableView。然後,您可以將IBOutlets添加到UIButton/UILabel/UISwitch等等,並且可以隨意使用您的ViewController,而不侷限於僅使用該視圖控制器內部的表視圖。我幾乎總是在使用UITableViews/UICollectionViews時使用這種方法,即使我將tableView/collectionView設置爲整個視圖的大小,因爲我喜歡通過UITableViewController/UICollectionViewController使用UIViewController的自由度。

*注意numberOfRows()不是必需的,但我總是重寫它,只是一種習慣在這一點上。此外,你對iOS開發聽起來很陌生,所以如果你還沒有,那麼在啓動並運行你的tableView之後,接下來的事情就是在後臺線程上從你的API中提取數據,以保持mainThread對用戶響應打開你的用戶界面,DispatchQueue。如果您正在顯示來自API的圖像,這非常重要。

+0

謝謝雅各布。我連接故事板上的委託和數據源。只是再次確認,他們仍然在那裏。你對我的iOS相當新鮮是正確的。現在只進行一個月。感謝有關API –

+0

的信息沒問題,只要在多線程工作時一定要注意比賽條件。你可以用多線程做很多很酷的事情,並且在可用時填充tableView的動畫數據。一個快速的谷歌搜索會給你很好的Tuts這種事情,玩得開心:) –