简体   繁体   中英

Manipulating data through table view cells iOS swift

firstly thank you in advance for any help and secondly I am very new to do the iOS development and swift so please bear with me and forgive me if I ask any stupid question.

MY ISSUE:

I created a table view and created an array of numbers. I iterated through the array and displayed each number in the array in a different table view cell. Now what I want to do is either have a button my table view cell or a check mark. And when ever I tap the button or the check mark the number that is being displayed on the table cell from the array is selected and then I tap another button or a check mark on a different cell and that number also gets selected and when I click the "done" button the both numbers are added and displayed on my root view.

Its kind of like a order taking app you can think of each cell as displaying the price of a food item and you can selected multiple food items and once you click done it adds up the price and displays it.

Im not looking for any advanced techniques, anything that can help me do this will be much appreciated. Again sorry for any stupid questions and thank you.

Key things to look at: UITableView has a var called allowsMultipleSelection . You want to set that to true (it's false by default.)

If you want to change the way a cell looks when it is selected, then you can either look into the table view's delegate didSelectRowAtIndexPath and didDeselectRowAtIndexPath . Or if you have your own subclass of UITableViewCell, you can override the setSelected:animated: method.

The basic idea is to give the user a button to say when (s)he is done selecting cells, then look at the tableView's indexPathsForSelectedRows to find out which items were selected.

This is how you should break it down:

1) Implement the didSelectRowAtIndexPath delegate method after the table has been populated:

 func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {

2) Within that method set the checkmark for the row:

cell.accessoryType = UITableViewCellAccessoryType.Checkmark

3) Declare a class Array:

var numberArray: [Int]

4) Finally, implement an array extension for the addition. Call it when tapping the done button: How can we create a generic Array Extension that sums Number types in Swift?

There's some fine tuning depending on your implementation but I hope this gives you a head start.

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