我正在创建一个项目,我在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我会读它并试图找出 –