2017-07-29 39 views
0

我花了幾個小時試圖找到一個解決方案,但我總是沒有結果。TableView內的分段控件

我正在嘗試在TableView內部爲段控制的兩個視圖創建一個段落控件。

https://www.youtube.com/watch?v=dw7kytoA3KU&t=198s是我嘗試完成的一個完美例子,但是用戶放入了1個CustomCell並且代碼有點混亂。

我的第一個CustomCell包含圖像,名稱和數字。 我的第二個CustomCell只包含一個圖像。

所以我的問題是...

  1. 我應該如何創建兩個陣列兩個CustomCell?的就是我試圖完成

自定單元1:

   let names = ["Pizza","Soup","Tacos","Chicken", "Beef"] 
       let image = [UIImage(named: "Pizza1"), UIImage(named: "Soup1"), UIImage(named: "Tacos1"), UIImage(named: "Chicken1"), UIImage(named: "Beef1")] 
       let numbers = ["1", "2", "3", "4", "5"] 

自定單元2:

  let photo = [UIImage(named: "Pic1"), UIImage(named: "Pic2"), UIImage(named: "Pic3")] 
  • 從我研究過的,當它'有時候需要將CustomCell文件和兩個CustomCell放入我的代碼中,這是否正確?

    let nib = UINib(nibName: "CustomCell", bundle: nil) 
    tableView.register(nib, forCallReuseIdentifier: "CustomCell1") 
    tableView.register(nib, forCallReuseIdentifier: "CustomCell2") 
    
  • 應該爲此返回什麼?

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int

  • 這部分是我得到什麼進入

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

  • 總結困惑:我試圖創建一個的tableView段控制有2 CustomCells。我希望該段的第一個視圖是CustomCell,它具有圖像,名稱和編號。該部分的第二個視圖只有圖像。

    謝謝!

    +1

    TableView ...裏面...段控制? – Matthew

    +0

    @Matthew ...我的不好,一個UISegmented控件在桌面視圖中 – William

    回答

    0

    我應該如何爲兩個CustomCell創建兩個數組?

    let names = ["Pizza","Soup","Tacos","Chicken", "Beef"] 
    let image = [UIImage(named: "Pizza1"), UIImage(named: "Soup1"), UIImage(named: "Tacos1"), UIImage(named: "Chicken1"), UIImage(named: "Beef1")] 
    let numbers = ["1", "2", "3", "4", "5"] 
    

    有三個陣列將工作,但有一點奇怪。迅速結構和一個數組可能是比較容易的工作:

    struct Food { 
        var name: String 
        var image: UIImage 
        var number: Int 
    } 
    
    let foods = [ 
        Food(name: "Pizza", image: UIImage(named: "Pizza1"), number: 1), 
        Food(name: "Soup", image: UIImage(named: "Soup1"), number: 2), 
        Food(name: "Tacos", image: UIImage(named: "Tacos1"), number: 3), 
        Food(name: "Chicken", image: UIImage(named: "Chicken1"), number: 4), 
        Food(name: "Beef", image: UIImage(named: "Beef1"), number: 5) 
    ] 
    

    從我的研究,當它的時間來呼籲CustomCell文件和兩個CustomCell到我的代碼,這會是正確的?

    let nib = UINib(nibName: "CustomCell", bundle: nil) 
    tableView.register(nib, forCallReuseIdentifier: "CustomCell1") 
    tableView.register(nib, forCallReuseIdentifier: "CustomCell2") 
    

    幾乎...如果你有兩個自定義單元,我希望看到兩個筆尖:

    let nib1 = UINib(nibName: "CustomCell1", bundle: nil) 
    tableView.register(nib1, forCallReuseIdentifier: "CustomCell1") 
    let nib2 = UINib(nibName: "CustomCell2", bundle: nil) 
    tableView.register(nib2, forCallReuseIdentifier: "CustomCell2") 
    

    我應該爲這個被退回?

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
    

    您的表細胞的數量。因此,它可能是:

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
        return foods.count 
    } 
    

    這部分是我得到什麼進入

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

    這是你的應用程序出隊細胞,並配置其內容之前迷茫它出現在表中:

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
        let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell1", for: indexPath) 
        cell.nameLabel.text = foods[indexPath.row].name 
        return cell 
    } 
    

    我還沒有確定你打算如何處理SegmentedControls ...你希望每個單元格包含一個分段控件嗎?這將是非常不尋常的。

    0

    首先我建議你通過一些關於如何使用UITableViews和UISegmentedControl的教程,因爲它看起來像你是iOS開發的初學者。現在回答你的問題:

    #1. How should I create two array for the two CustomCell? 
    

    我建議你研究在Swift中創建NSObject。使用此功能,您可以在對象上設置諸如名稱,圖像,數字等屬性。然後,您可以收集對象的集合,並在您的數據源上將它們用於表格。

    #2. Regarding Custom UITableViewCells. 
    

    是的,你需要註冊自定義的UITableViewCells供你使用它們。或者做故事板的方式。

    #3. func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
    

    您在此委託方法中指定應出現在您的UITableView中的行數。例如,如果你有一個食物對象的數組,你會返回foods.count。

    #4. func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
    

    這是你創建和定製將在你的UITableView的行上顯示的單元格的部分。

    如果你想要一個UITableView和UISegmentedControl在一起,我建議通過在你的UIViewController視圖中添加一個UISegmentedControl和兩個UITableViews來實現最簡單的方法。根據選擇的段更改可見的UITableView,您可以在UISegmentedControl的值更改事件上捕獲它。