我正在創建一個項目,我在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()
}
我猜這個教程將幫助您管理多個/嵌套的區段:http://sapandiwakar.in/nested-sections-in-uitableview/ – Santosh
非常感謝你@Santosh我會讀它並試圖找出 –