get selected button from all the cells - ios

I am new to swift and iOS. I am stuck at one point of my development. I hope any one of you. The problem is
I have a table view cell, in which I am having one question and two buttons with yes or no title. Initially both buttons will not be selected(white background). After click on button it should be selected(blue background). Any one of the button should be selected at the time. I have 5 of these cells totally.
At footer of tableview I have continue button. After answering all these questions the button will be enabled. Here, I need to check how many yes and no available.
I have implemented the UI and everything. But at continue button action I couldn't get the number of yes es and nos. I tried but not able to figure out. Could anyone help me please.

I am assuming that you are using Model to display the cells in the tableView
For example:
struct Question {
var text: String?
var selectedAnswer:Bool? // make it true if user selected yes or make it false if user selected no
func getAllAnswers(questions:[Question])->[Bool] {
return questions.compactMap{ $0.selectedAnswer }
}
}
By using getAllAnswers() you will get an array and get the count from that

Related

Can I choose only portion of a row in UITableView to be clickable(selectable) in Swift?

So I have a table in the accordion style. When a row is clicked, five radio buttons are displayed. Then, users should be able to click on those buttons. It successfully displays the buttons, but when the buttons are clicked, instead of highlighting the selected button, it shrinks the row itself. I think this is probably because I implemented each set of the label part and the option part as a single row, just in different UIViews. I believe clicking on the view that contains buttons is overridden by clicking on the row(label + options). Its really hard to explain, but I think a solution could be restricting selectrow action of the table to only certain portion of the row.
Is there a way I could achieve this? If I cannnot, is there other ways to implement the accordion table to be used as I described?
I am having a really hard time trying to explain this. If you have any questions, please let me know.
table
As per my understanding you van give a try to below steps:
Disable the row selection so that you can avoid row selection. Usecell.selectionStyle = .none
Secondly for your button in row set the IBAction in controller class and in Storyboard connect those actions to specific buttons.

iOS News App like Animation

How can I achieve the following animation?
https://ibb.co/mZgAGF
Consider the list of cells in UITableView/UICollectionView.When the user taps on an UITableViewCell/UICollectionViewCell, the detail page animates/opens from the selected cell and when we go back, the detail view goes inside the selected cell.
Please let me even if we've any existing libraries.
Thanks in advance.

How to select ALL UICollectionView cells like in the iOS Photos app using Swift?

I can't seem to get this to work for me and I haven't found any other answers to similar questions that work.
I am working with Swift and need to know how to be able to have a "Select All" button in my app select all visible & non-visible cells just like the stock Photos app on iOS.
My app is dealing with photos too, and so each cell has two UIImageViews, one that is the photo in question, and another that is a hidden UIImageView that appears upon selection of an image (it is just a check mark).
The code I've been using (which won't work) is this:
for cell in self.collectionView.visibleCells() as! [ImageCell]{
if cell.checkMark.hidden{
cell.checkMark.hidden = false
}
else{
cell.checkMark.hidden = true
}
}
I think it's good to understand how the control works. UICollectionView and UITableView both only create and display enough cells to fit on screen (and maybe a couple more for scroll buffer). This is in order to handle the case where an app may have a large number of data. For example, if a user has 1000 photos, performance would be awful if 1000 were actually displayed on and off screen.
That being said, you should handle selection through models. There are many ways to do this, but one way is:
Have each cell be represented by a custom object that has at least a photo and isSelected as a property. Every time the user selects or deselects, the cell, simply update the isSelected property. In collectionView:cellForItemAtIndexPath:, display the checkmark based on the isSelected property. Select all by simply looping through the array and updating that one property, then call reloadData to update it for every cell.
Because you updated the array and the UICollectionView is based on the array, every cell will be selected as it gets shown on screen.

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.
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.
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.

iOS 7 - UIPickerView over UITableView

I can't find anything else on the internet about this specific topic. Everyone else wants to know how to do inline UIPickerView, but there is nothing on just having a UIPickerView appear over a table.
I have a TableView with a bunch of populated cells. I also have a defunct button in the top left of the screen. What I want is to be able to press the button, bring up a Picker and sort the TableView's cells based on what is selected.
I've been looking around and trying for ages and I just can't find anything detailing a solution to my problem.
I would try using ActionSheetPicker-3.0
It makes it easy to present a UIPickerView over your current view without having to use a delegate as it uses callback blocks.

Resources