Ios 点击按钮时,使用新节填充UItableView

Ios 点击按钮时,使用新节填充UItableView,ios,swift,uitableview,Ios,Swift,Uitableview,我正在做一个项目,我在ViewController 1中创建了一个TableView。 在这个VC1中,我还创建了显示popupView的按钮(我们称之为popupButton)。 popupview包含一些数据,即由按钮表示的膳食。点击按钮后,我从popupview中收集所选的膳食,并通过委派将其发送回VC1。我成功地做到了这一点,我可以在UITableView上显示所选的膳食。问题是,我想根据所选的客户机(在VC1中,我有代表每个客户机的UIbuttons)填充tableView,如下所示:

我正在做一个项目,我在ViewController 1中创建了一个TableView。 在这个VC1中,我还创建了显示popupView的按钮(我们称之为popupButton)。 popupview包含一些数据,即由按钮表示的膳食。点击按钮后,我从popupview中收集所选的膳食,并通过委派将其发送回VC1。我成功地做到了这一点,我可以在UITableView上显示所选的膳食。问题是,我想根据所选的客户机(在VC1中,我有代表每个客户机的UIbuttons)填充tableView,如下所示:

  • 客户1
    • 一餐
    • 膳食B
  • 客户2
    • 膳食C
    • 第十餐等
如果选择了按钮客户端1并点击了菜单A,则创建C1部分作为标题,菜单A菜单B等等。然后,当我点击客户端2并选择“其他膳食”时,将这些膳食添加到一个标题为客户端2的新部分。目前,如果选择了客户端1,我会将其作为标题。但是,如果我点击客户端2并选择一个新的膳食,它将把标题更改为客户端2,并且不会创建新的部分。此外,我的代码还可以工作,即即使没有选择任何客户,我也可以将膳食添加到我的餐桌上(目前为止这很好,但我希望它仅在选择客户时工作)

在下面的代码中,我只展示了有助于理解我目前所做工作的部分。如果有任何想法,我都会非常感激,因为我已经找了一段时间了。我正在考虑使用struct,但我不知道如何将它与我的案例结合起来。我看到的所有例子似乎都是关于静态的,而我正在寻找动态的东西。感谢亲爱的读者花时间阅读我的问题并帮助一个“绝望的灵魂”


我尽我所能回答你需要什么

因此,我们希望能够有多个部分。所以我们需要一个变量来改变tableView中的节数

var numberOfSections: Int = 1

func numberOfSectionsInTableView(tableView: UITableView) -> Int {

     return numberOfSections
}
这样,我们可以根据需要更改
numberOfSections
,以便在TableView中有1或2个分区

然后,我们需要有多个数组来定义
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 
}
我们还需要两个客户字符串来填写
标题forheaderin部分

var myFirstCustomer = ""
var mySecondCustomer = ""

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

        if section == 0 {
            return myFirstCustomer
        }

        if section == 1 {
            return mySecondCustomer
        }
}
现在,我们可以填充阵列并告诉我们的应用程序是否需要一个或两个部分,这取决于这样的客户数量。我们还需要知道是否为客户1或客户2选择弹出菜单,但我目前不知道您的弹出菜单是什么样子,但代码如下:

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的情况下编写了这段代码,所以它可能并不完美。但它肯定会让你知道我将如何处理这个问题。

我会尽我所能回答你需要什么

因此,我们希望能够有多个部分。所以我们需要一个变量来改变tableView中的节数

var numberOfSections: Int = 1

func numberOfSectionsInTableView(tableView: UITableView) -> Int {

     return numberOfSections
}
这样,我们可以根据需要更改
numberOfSections
,以便在TableView中有1或2个分区

然后,我们需要有多个数组来定义
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 
}
我们还需要两个客户字符串来填写
标题forheaderin部分

var myFirstCustomer = ""
var mySecondCustomer = ""

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

        if section == 0 {
            return myFirstCustomer
        }

        if section == 1 {
            return mySecondCustomer
        }
}
现在,我们可以填充阵列并告诉我们的应用程序是否需要一个或两个部分,这取决于这样的客户数量。我们还需要知道是否为客户1或客户2选择弹出菜单,但我目前不知道您的弹出菜单是什么样子,但代码如下:

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的情况下编写了这段代码,所以它可能并不完美。但它肯定会让你知道我将如何处理这个问题。

跟随@David Seek inspiration

我创建了一个获取客户机数量的变量,因为客户机的数量将对应于表视图的节数。此数字的变化取决于是否选择了客户端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")

        }
然后我创建了两个空字符串,用每个客户选择的食物填充:

var firstarray = [String]()
var secondarray = [String]()
在tableview中实现后,如下所示:

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)")
       } 
我还没有为标题部分创建单元格。我先运行了这个程序,看看我得到了什么,它成功了!:

当我选择client1时,这意味着我有一个部分,所以我单击弹出按钮,我可以添加我想要的食物。然后,当我选择Client 2(标签现在是2)时,我有2个部分,因此我还可以添加我想要的食物,我得到以下结果:

  • 客户1
  • 一餐
  • 膳食B

  • 客户2

  • 膳食C
  • 膳食D
但是,当我决定向客户端1添加一顿新的一餐(比方说一餐E)时,它会删除我为客户端2添加的所有内容,并显示以下内容:

  • 客户1
  • 一餐
  • 膳食B
  • 膳食E
我认为这与这样一个事实有关:当我重新选择客户端1时,客户端的数量从2变为1,它删除了之前添加的所有内容。所以我现在想一些方法来解决这个新的有趣的问题

我还注意到,如果我决定先点击按钮2,这意味着在客户1之前接受客户2的订单(一顿C餐),它会创建两个部分,第一个部分为空(见下文)

  • 客户1
  • 空白
  • 客户2