Swift 2.1 Selecting Options from Image Icons in a View Controller - ios

If I wanted to have a screen with a number of options displayed via icons to present to the user to select from. What would be the best way to do this?
For instance if I had 6 different images with varying price ranges like $25 - $35, $35 - 50, $50 - $75 and so on and I wanted them to select one of these ranges, what would one suggest I use in this situation? A tableview? A collectionview? Is there a way to use a collectionview in this manner?

It is totally depend on your app requirement and UI. you can use tableview, collectionView, or scrollview.
you just need to manage the selection of the item. For that you should use NSArray of NSDictionary. Where NSDictionary having a key called selected which is set to true when item get selected. When any other object get selected you make a loop which make all object selected to false. and then set the selected object to true.

Related

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.

iOS Swift How to Extract Strings from All Selected Rows in a TableView

I want to loop through a TableView and extract the text from all the selected rows. I suppose I "could" create and maintain a special array that is updated every time a row is selected/deselected using the didSelect/didDeselectRowAtIndexPath methods. But creating a separate array seems like an extra step. Is there no way to let the TableView itself serve as the array and then simply loop through it and get the selected rows? What would the code look like? I'm new to Swift, so this might be a silly question.
Part of the problem is that cells are supposed to be reused, and when used this way it is not possible to loop through them all. You could get around this by using a unique reuse identifier for each cell, such as the indexPath itself or some underlying unique id in your model. Then, you could indeed loop through all cells and retrieve whatever state you desired from each.
You would, however, find your application crushed under the weight of too many cells being instantiated and kept in memory. If you don't have many cells you won't be killed, but try it with a big data set and your app will enjoy a very quick death.
It is far more efficient to store one array with a bunch of id's than a large number of memory-intensive UITableViewCells.
As mentioned in comments, you should work with underlying datasource, not the table itself.
For example if your table shows rows from Array, it is way more faster to retrieve strings directly from that array than creating UITableViewCells and get strings from them.
Get indices of selected rows using UITableView's property indexPathsForSelectedRows.
Query datasource for each row.
As has been said the tableview only handles displaying, your datasource is what powers the data shown if you think about it.
Plus as said before the tableview dequeues cells as they scroll on and off the screen.
The best way to achieve what you want is to add a property to your datasource for each element that will allow you to filter out the select properties easily.
How are you storing the state for each selected cell currently? As this is the same functionally you would use to be able to generate your selected text array.

how to connect textfield with table view of listed options instead of keypad

I need to be able to disable the keypad in a textfield, and instead when the user taps on it to start editing, a new table view will appear presenting a list of possible strings with which the textfield should be filled.
Anyone has any suggestions on how to achieve this?
Thanks in advance
p.s. I tried already but this functionality cannot be nicely implemented with a picker in my case, as there are too many options to choose from and (more importantly) each one of them is a rather long string and cannot appear entirely in a picker.
I believe you just need a regular cell that, when tapped, pushes a new detail UITableViewController with all the options to choose from. Once an option is chosen, set the cell's textLabel to whatever option have been selected.
If you'd like to go for an easier path, then you should also probably check the free Sensible TableView framework, as it has these kinds of cells out of the box (called selection cells). You just pass on your strings array to the selection cell and it will automatically display the detail view, and even assign the selected option to one of your object's properties if you wish. Should save you some good amount of manual work. Good luck!

"Compact" view in iOS

I have a view in iOS (iPhone) that have multiple components, organized in sort of a stack way (one in top of the next). Those are user account properties, some could be blank.
So, I have in my view the components layout like this:
UITextField1 (Name)
UITextField2 (Location)
UITextField3 (Age)
UITextView1 (Bio)
UITableView (user entries).
Some of the fields could be blank. Instead of having blank spaces for the blank fields I would like the next field to move upper.
This is like this question of flowlayout: What is the best/easiest way to create 'flow layout' type layout in iOS.
I can only see two ways of dealing with this:
Creating a function that traverse all the UIViews and determines which ones are blank and move the following upper.
Creating a UITableView and use different cell heights for cells whose content is empty.
Ideally there would be a component, but I cannot find it (basically some sort of stack/flow layout).
Anyways, I believe that I am going to implement the option #1 above, but I don't know if there is an "standard" way of accomplishing this (I honestly don't even know the proper term to look for this feature).
Thanks.
I would lean more in the direction of your second choice by using a UITableView but not the way you propose.
This would be my approach using a UITableView:
Create a UITableViewCell (or custom cell) for each one of my
components and assign a tag value to each, we'll use these later. You can do this in viewDidLoad.
Add code in numberOfRowsInSection to check to see which fields have
data values present. Return the total count of the number of fields
with data values
In cellForRowAtIndexPath again, check if data exists for that field
If so, check to see if the cell created in step one has already been created or not (if not, create)
If not, increment a counter of some sort to increase your tag value and find the next field that has a value. Once found, use that "tag/index" number to "return" the proper cell.
In the end, you have a UITableView only displaying the fields with data.

Possible to allow a uitableview to allow multiple and single selections?

Essentially I have a list of items, I want to have some type of checkbox on the end where they can select multiple items. I also need them to be able to just select one of the rows and have it open up a detail view. Is this possible? Or do I need to do something like they do with the messages where you choose the edit button to select them and show an additional button to take action on the selected?
Set your cell's UITableViewAccessoryType property to UITableViewCellAccessoryCheckmark to make a check mark show up on a certain cell, you'll have to keep track of which cell's are checked on your own though, since this property doesn't track.
So have an array or something that represents which cells ar checked and which aren't, then in your didSelectRowAtIndexPath: method you can modify the data in the array to reflect the cell's selected state, and call [tableView reloadData] so the checkmark shows up right away.
I found the answer, there's an option to allow multiple selections on edit mode.

Resources