2016-09-22 67 views
1

我正在創建一個項目,我在ViewController 1中創建了一個TableView。 在這個VC1中,我還創建了一個按鈕(讓我們稱之爲popupButton) 。 彈出式視圖包含一些數據,即用按鈕表示的用餐。點擊按鈕時,我收集從彈出式視圖中選擇的膳食並通過代表團將其發送回VC1。我設法做到這一點,我可以顯示在UITableView上選擇的膳食。問題是我想要填充的tableView取決於所選擇的客戶端上(在VC1我有表示每個客戶端UIbuttons)如下:當按鈕點擊時用新節填充UItableView

  • 客戶端1
    • 餐甲
    • 食物B
  • 客戶端2
    • 餐ç
    • 餐X等

如果按鈕客戶端1被選擇且該膳食A被抽頭創建部C1作爲報頭和膳食膳食B等。然後,當我點擊客戶端2並選擇其他餐食時,將這些餐點添加到標題爲客戶端2的新部分。目前,如果選擇了客戶端1,則將其作爲標題。但是,如果我點擊客戶端2並選擇一頓新餐,它會將標題更改爲客戶端2,並且不會創建新的部分。另外我的代碼工作也就是說,即使沒有客戶端已經在所有被選中,我可以添加飯菜,我的表(這是很好的,到目前爲止,但我會把它想當客戶只選擇工作)

在下面代碼我只顯示幫助理解我到目前爲止所做的部分。任何想法,因爲我一直在尋找一段時間,會真正讚賞。我正在考慮使用結構,但我沒有任何關於如何將它與我的案例混合的線索。我所看到的所有例子似乎都是關於靜態案例,而我正在尋找動態的東西。感謝親愛的讀者花時間閱讀我的問題,幫助「絕望的靈魂」。

var meal: String? // this var is used to collect the name of the meal 

var mycustomer:String = "" // this var gets the name of the customer 
    @IBAction func clientselected(sender: UIButton) { 
    if sender.selected { 
     sender.backgroundColor = UIColor.lightGrayColor() 
     sender.selected = false 

     print ("the button is unselected") 
     } 

    else { 
     sender.backgroundColor = UIColor.redColor() 
     sender.selected = true 
     print ("the button is selected") 
     let clientname = sender.titleLabel!.text 
     mycustomer = clientname! 

     } 

var myarray = [String]() // Create an empty array of strings that we can fill later on with the meals selected 


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

    return myarray.count // Gets the number of strings in the array 
} 

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

    let cell = tableView.dequeueReusableCellWithIdentifier("Leschoix", forIndexPath:indexPath) as UITableViewCell! 
    cell.textLabel?.text = myarray[indexPath.row] 
    return cell 

} 

func tableView(tableView: UITableView, titleForHeaderInSection section: Int)-> String? 
{ 
    return "\(mycustomer)"} 

func foodselected(valuesent:String) { // this func gets the name of the meal tapped in the popUpView through delegation 
    let meal = valuesent 
     print("OrderViewcontroller\(meal)") 
    myarray.append(meal) // each meal selected will be added up to the empty myarray 
     print ("Here is the food array \(myarray)") 
    self.TableView.reloadData() 

} 
+0

我猜這個教程將幫助您管理多個/嵌套的區段:http://sapandiwakar.in/nested-sections-in-uitableview/ – Santosh

+0

非常感謝你@Santosh我會讀它並試圖找出 –

回答

0

我試着回答你,據我瞭解你需要什麼。

所以我們希望能夠有多個部分。所以我們需要一個變量來改變tableView中的部分數量。

var numberOfSections: Int = 1 

func numberOfSectionsInTableView(tableView: UITableView) -> Int { 

    return numberOfSections 
} 

這樣,根據我們的需要有1個或2個部分我們TableView中,我們可以改變numberOfSections。然後我們需要有多個數組來定義numberOfRowsInSection。否則,我們每個部分都會有完全相同的元素。如果我們使用你的當前版本。這兩個部分將具有完全相同的內容。

var sectionOneArray = [String]() 
var sectionTwoArray = [String]() 

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

if section == 0 { 
    return sectionOneArray.count 
} 

if section == 1 { 
    return sectionTwoArray.count 
} 

    return 0 
} 

我們還需要兩個客戶字符串來填充titleForHeaderInSection。現在

var myFirstCustomer = "" 
var mySecondCustomer = "" 

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 

     if section == 0 { 
      return myFirstCustomer 
     } 

     if section == 1 { 
      return mySecondCustomer 
     } 
} 

我們可以填寫陣列,並告訴我們的應用程序,如果我們想要一個或兩個部分依賴於這樣的客戶量。我們還需要有可能告訴我們是否爲客戶1或客戶2選擇Popup中的菜單,但是我目前不知道您的Popup是什麼樣子,但代碼會是這樣的:

func foodselected(valuesent:String) { 

    if popUpChoseForCustomerOne { 
      let meal = valuesent 

      sectionOneArray.append(meal) 
    } 

    if popUpChoseForCustomerTwo { 
      let meal = valuesent 

      sectionTwoArray.append(meal) 
      numberOfSections = 2 
    }  

    self.TableView.reloadData() 

} 

我沒有IDE的代碼,所以它可能不完美。但是,它肯定會給你我如何處理這個問題的想法。

+0

嗨@大衛尋求我試過你的解決方案,它的工作!爲了告訴我何時選擇顧客1或顧客2,每個按鈕都有一個標籤。所以我得到發件人標籤,我說客戶端數= sender.tag(因爲客戶端的數量決定了部分的數量)。某些我認爲你誤解的事情是,只有一個彈出按鈕來選擇餐點,所以當我點擊該菜單按鈕時,我可以訪問不同的餐點。最後,當我應用這種方法時,它帶來了一個新問題(我很高興自從它工作以來就有大聲笑)。我會在進一步的評論中解釋。再次感謝! –

+0

你非常歡迎好友 –

0

繼@大衛尋求靈感,

我創建了一個變量,得到客戶的數量,因爲客戶端的數量將對應表視圖的節數。此號碼根據是否選擇客戶端1(1部分),客戶端2(2部分)或客戶端3等等而有所不同。由於我分配給每個按鈕的標籤我得到的標籤如下圖所示:

var numberofclients:Int = 1 
@IBAction func clientselected(sender: UIButton) { 
    if sender.selected { 
     sender.backgroundColor = UIColor.lightGrayColor() 
     sender.selected = false 

     print ("the button is unselected") 
     } 

    else { 
     sender.backgroundColor = UIColor.redColor() 
     sender.selected = true 
     let clientname = sender.titleLabel!.text 
     mycustomer = clientname! 
     numberofclients = sender.tag 
     print ("the button \(numberofclients) is selected") 

     } 

然後創建了2個空字符串,我充滿了每一個客戶選擇了一頓:

var firstarray = [String]() 
var secondarray = [String]() 

而且在實現代碼如下實施後,它看起來像這樣:

func numberOfSectionsInTableView(tableView: UITableView) -> Int { 


    return numberofclients 

} 

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

    if section == 0 { 

    return firstarray.count // Gets the number of strings in the array of C1 
     } 
    if section == 1 { 

     return secondarray.count // Gets the number of strings in the array of C2 
    } 


    return 0 
} 

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

    let cell = tableView.dequeueReusableCellWithIdentifier("Leschoix", forIndexPath:indexPath) as UITableViewCell! 
    if indexPath.section == 0 { // If you are in section 1, i.e, if client 1 is selected fill the first array with the meal selected 
    cell.textLabel?.text = firstarray[indexPath.row] 
     } 
    if indexPath.section == 1 // If you are in section 2, i.e. if client 2 is selected, fill the second array with the meal selected by the client 
    { 
     cell.textLabel?.text = secondarray[indexPath.row] 
    } 


    return cell 

} 

爲了填充單元格我用大衛的想法開關

func poutinechoisie(valuesent:String) { 
    let meal = valuesent 
     print("OrderViewcontroller\(mealselected)") 

    switch numberofclients { 
    case 1: 
    firstarray.append(meal) 
     print ("here is the first array \(firstarray)") 

    case 2: 
     secondarray.append(meal) 
     print ("here is the second \(c2array)") 
     } 

我還沒有創建標題部分的單元格。我首先運行程序,看看我得到什麼,它的工作!:

當我選擇客戶端1,這意味着我有1節,所以我點擊popupButton,我可以添加我想要的食物。後來,當我選擇客戶端2(即現在的標籤是2),我有2個部分,這樣我還可以加我想要的食物,我得到這個:

  • 客戶端1
  • 一頓
  • 食物B

  • 客戶端2

  • 餐ç
  • 餐d

但是,當我決定到一個新的餐添加到客戶端(比方說一頓飯E)它刪除我爲客戶端2中添加的一切,它顯示了這一點:

  • 客戶端1
  • 一頓
  • 食物B
  • 餐é

我認爲這事做,當我重新選擇客戶端1的客戶數​​從2變化爲1號,並刪除所有之前添加的事實。所以我現在想想解決這個新的有趣問題的一些方法。

我也注意到,如果我決定先點擊按鈕2,這意味着在客戶端1之前接受客戶端2訂單(一餐C),它將創建2個部分,第一個部分爲空白(請參見下文)。

  • 客戶端1
  • 空白
  • 客戶端2
  • 餐Ç

到目前爲止,它不打擾我,但我想有一個辦法只有一個部分和顯示例如:

  • 客戶端2
  • 餐Ç