我正在做一个项目,我在ViewController 1中创建了一个TableView。
在这个VC1中,我还创建了一个按钮(我们称之为popupputton)来显示一个popupView。
popupview包含一些数据,即用按钮表示的膳食。当点击按钮时,我收集从弹出视图中选择的食物,并通过委托将其发送回VC1。我成功地做到了这一点,我可以在UITableView上显示所选的饭菜。问题是,我想根据所选的客户端(在VC1中,我有代表每个客户端的ui按钮)填充tableView,如下所示:
客户1
A餐
B餐
客户2
C餐
X餐等。
如果选择了按钮Client 1,并且点击了meal A,则创建区段C1作为报头,meal A meal B等等。然后,当我点击Client 2并选择其他膳食时,将这些膳食添加到标题为client2的新部分。目前,我得到客户1作为标题,如果它被选中。但是,如果我点击client 2并选择一顿新的饭,它会将标题更改为client2,并且不会创建新的部分。此外,我的代码还可以工作,即即使没有选择任何客户,我也可以在我的桌子上添加饭菜(目前为止很好,但我希望它只在选择客户时工作)
在下面的代码中,我只展示了有助于理解到目前为止我所做的事情的部分。我已经找了一段时间了,如果有什么想法我会非常感激的。我正在考虑使用struct,但我不知道如何将它与我的案例相结合。我看到的所有例子似乎都是关于静态案例,而我在寻找动态案例。感谢你亲爱的读者花时间阅读我的问题,帮助一个“绝望的灵魂”。

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()

}

最佳答案

我尽量回答你,只要我明白你需要什么。
所以我们希望能够有多个部分。所以我们需要一个变量来改变tableView中的节数。

var numberOfSections: Int = 1

func numberOfSectionsInTableView(tableView: UITableView) -> Int {

     return numberOfSections
}

这样,我们就可以根据需要在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选择菜单,但我目前不知道您的弹出菜单是什么样子,但代码应该是这样的:
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的情况下编写了这段代码,所以可能并不完美。但这肯定会让你知道我会如何处理这个问题。

关于ios - 点击按钮时,用新的部分填充UItableView,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39628621/

10-12 14:31
查看更多