简体   繁体   中英

How to know which section is selected ? Swift

I'm working on a dynamic tableView with a lot of items, divided into sections , based on their selection. The sections are the client names, the items (ie the rows in each section ) are the foods selected by the client. So far I'm having some issues about how to update it.
I used a table with 4 buttons (C1,C2,C3,C4). Then when a client is selected i append it in an empty array (client) that i use for the header section.
I've also created 4 empty arrays for each section ( myarray , c2array , c3array , c4array ) that I fill with the item selected by the client but there's a confusion in a part of my code .

In order to fill the cell for each row I wrote this:

    let cell = tableView.dequeueReusableCell(withIdentifier: "Leschoix", for:indexPath) as UITableViewCell!

    if indexPath.section == 0 {
        cell?.textLabel?.text = myarray[indexPath.row]
        }

    if indexPath.section == 1  {
        cell?.textLabel?.text = c2array[indexPath.row]
    }

    if indexPath.section == 2 {
        cell?.textLabel?.text = c3array[indexPath.row]
    }

    if indexPath.section == 3 {
        cell?.textLabel?.text = c4array[indexPath.row]
    }

        }

After running a quick simulation it I get this


  1. C1: meal A meal B (I select C1 and chooses 2 meals A and B)

  1. C2: meal C meal D

Now when I try to add a meal E to C1 again (I select C1 button) I get:


  1. C2: meal C meal D meal E instead of updating C1

So my code is saying if there's 1 section fill the array with the text, if there are 2 section , fill it with c2array , and so on..and in this case we have 2 sections so it won't update the 1st one. which means I can't update the first section. How can I say If you're in the section i then update the appropriate array ?. I'm looking for a way to say that whenever client i is selected then i'm in section j and fill/update the corresponding array accordingly. Thank you for your help!

I assume that the flow is to select a button (C1/C2/C3/C4) and then you select any meal (A/B/C/D/E...) to be added.

Here's a solution.

Maintain a variable in your class selectedCustomerIndex: Int and when button C1 is selected update selectedCustomerIndex to 0, for C2 set it as 1, for C3 set it as 2 and so on.

So now when you add any meal, add it to the appropriate array (myarray, c2array, c3array...) depending upon the value of selectedCustomerIndex

Something like this:

switch selectedCustomerIndex {
    case 0:
        //add to myarray
    case 1:
        //add to c2 array.
    .
    .
    .
    .
    default: //do nothing.
}

And then reload your tableview.

Personally I feel there are a lot of better and scalable approaches to structure the solution to what you're trying to build in your app, but this should serve well as a quick-fix. Hope it helps!.

As you said you are working in tableview following snippet may help you

your button on tableview cell :

cell.yourButton.tag = indexPath.section
cell.btnselectedClient.addTarget(self, action: #selector(buttonAddMeTap(sender:)), for: UIControlEvents.touchUpInside)

Detect which section button got tap and add element to your array according to your requirement

func buttonAddMeTap(sender:UIButton) {

    switch sender.tag {
    case 0:
        print("first")
    case 1:
        print("Second")
    case 2:
        print("third")
    case 3:
        print("fourth")

    default: 
        print("default")
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM