2016-09-22 61 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
  • 餐Ç