Question

How can I implement a UISwitch in every cell of a UITableView, in order to use the tableView as a sort of checklist, which I can then gather the checked items to send to a backend, to 'purchase' the items?

The list of items may change and is located on the app backend, I have already implemented the parsing of the items and the building of the tableview, but I'm just not sure how to assign the switches when the length of the tableview is uncertain.

I'm new to Objective-C and XCode, and couldn't find any answers anywhere, so thanks for any help.

Was it helpful?

Solution

I am assuming here that your data is stored in an array.

  • In each UITableViewCell, where you have added the UISwitch, set it's tag to same as it's indexPath.row.

    mySwitch.tag = indexPath.row;
    
  • You can easily get the index of the object for which you had set the switch, using the the tag stored.

    [myArray objectAtIndex:mySwitch.tag];
    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top