Ios 点击按钮时,使用新节填充UItableView
我正在做一个项目,我在ViewController 1中创建了一个TableView。 在这个VC1中,我还创建了显示popupView的按钮(我们称之为popupButton)。 popupview包含一些数据,即由按钮表示的膳食。点击按钮后,我从popupview中收集所选的膳食,并通过委派将其发送回VC1。我成功地做到了这一点,我可以在UITableView上显示所选的膳食。问题是,我想根据所选的客户机(在VC1中,我有代表每个客户机的UIbuttons)填充tableView,如下所示:Ios 点击按钮时,使用新节填充UItableView,ios,swift,uitableview,Ios,Swift,Uitableview,我正在做一个项目,我在ViewController 1中创建了一个TableView。 在这个VC1中,我还创建了显示popupView的按钮(我们称之为popupButton)。 popupview包含一些数据,即由按钮表示的膳食。点击按钮后,我从popupview中收集所选的膳食,并通过委派将其发送回VC1。我成功地做到了这一点,我可以在UITableView上显示所选的膳食。问题是,我想根据所选的客户机(在VC1中,我有代表每个客户机的UIbuttons)填充tableView,如下所示:
- 客户1
- 一餐
- 膳食B
- 客户2
- 膳食C
- 第十餐等
我尽我所能回答你需要什么 因此,我们希望能够有多个部分。所以我们需要一个变量来改变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
- 一餐
- 膳食B
- 膳食E
- 客户1
- 空白
- 客户2