How to have different elements in collection view? [duplicate] - ios

This question already has answers here:
Displaying two different cells in a collection view - Swift 2.0 iOS
(2 answers)
Closed 5 years ago.
so I try to have collection view but been thinking is it possible to have different element in each cell view? For Example: in the first cell I have the whole cell covered by UIImageView while some specific cells only got UITextView.
Here's an example that I'm talking about, Medium iOS App.

Create several prototype cells, each formatted with whichever UI elements you want, and dequeue the appropriate cell for each different type of data you want to display.

Related

save data in array coming from 2 different uitableviews in ios swift

Recently i am facing an issue in saving data of Uitableview inside Uitableview cell in ios swift
Senario
Questions asked in above pictures is one table view and this table view cell contain 1 label for question text and uitableview that show possible answers provided by admin and user is allowed to select one.i am enable to display data perfectly but facing an issue when trying to save answers of every question in one array.it always store the value of last questions answers.
Why are you using two different UITableView? Any specific reason? You can get data from different resources but there is no need to use two table views.
You should use a section header view to show the questions & sections rows as options(answer option).
You can dynamically add or delete the options(rows) from any section(question). Also, there will not be the above scenario if you implement it using this easy approach.
Let me know in case of any query.

how can i add a call button to cells [duplicate]

This question already has answers here:
detecting uibutton pressed in tableview: Swift Best Practices
(12 answers)
Calling a phone number in swift
(25 answers)
Closed 3 years ago.
I'm trying to develop my first app on IOS platform and i want to add a call button to different cells and give each cell a different number to call because I want to programmatically add number to each cell to call ((for example I want cell A to call only the number 123 when I press the call button and I want the Cell B to only call 456 when I press the call button in that cell , I have a service class where my data is stored and I have DeatilCell class to update my cell with different data and another class to pass my data to the cells
I tried to search for some answers on the internet but I couldn't find anything
You need to do 2 things:
Have the specific phone number based on the index of the cell.
Call it.
There are many approaches to get a value based on events inside UITableView or UICollectionView cells, just pick one.

Two different UITableViewCells with same xib [duplicate]

This question already has an answer here:
One xib, several sub classes
(1 answer)
Closed 4 years ago.
I have two UITableViewCells that looks exactly the same, but each one has a completely different logic.
So I want to create two UITableViewCell, with the same xib.
The logic of each cell is different and I don't want to have code split with ifs.
How do I do that?
If its a same UI than you shouldn't create two UITableViewCell.
What you can do it write two different updateView method in a same uitableview class.
This way you can reuse the code and also if there is design change you just need to change in one place

UITableView for a list of Controls [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I am trying to understand when one should/should not use a UITableView.
I do know, if there is a table of information UITableView would be the perfect fit. But what if there are rows of "controls" ?
See the image below.
What are some of the advantages and disadvantages of using a UITableView to achieve layout above?
What alternatives to a UITableView exist for achieving such a layout?
This comes down to the question of how many controls/rows you will have and what they represent.
Dynamic Number of Controls Based on Model
A tableview is the perfect solution. The complications with having controls in a table view cell that is dynamically generated:
1) You cannot add IBActions for controls in table view cells. You will need to add a target/action to each control when the cell is generated.
2) Your action method will need a way to determine which element in your model the cell is displaying when selected (since cells are reused, controls will correspond to many model objects). To do this you need the index path in your model corresponding to the selected control. This can be done as follows (assuming a button but should work for any control):
Determine the control's position in the tableview:
let buttonPosition = button.convertPoint(.zero, toView: tableView)
Find the index path
let indexPath = self.indexPathForRowAtPoint(buttonPosition)
Then query your model for the object at the index path and respond accordingly.
Finite Number of Controls
The above is needlessly complex if you have a finite and known number of controls at runtime. In that case the above layout can be easily achieved with a UIStackView.
See StackView Documentation
Having controls in tableViews is perfectly fine.
You may want to know that tableviews have scrollable content and selectable cells, so your interactions will be slightly different than if you directly put your controls in a view.
Row selection and bouncing bounds can be annoying, as well as the delayed tap while touching inside a scroll view.
But if you need your controls to be dynamically shown depending on your settings, it can be done with the tableview data source or delegate methods.

How to create a collection view with static cells only? [duplicate]

This question already has answers here:
Is completely static UICollectionView possible?
(3 answers)
Closed 7 years ago.
I want to add a vertical collection view that contains 7 static cells. I know how to do it dynamically, but i don't see a reason to do so.
Do i have to use the cellForRow method?
I Added a collection view to my viewController, and added 4 cells, wired up the delegate from the collectionView to the vc (using storyBoard), but when i run it it shows me an empty collectionView.
I did set up the numberOfSections to return 1 and the numberOfCellsInSection to return 7. So did i miss anything? or do i have to conform to DataSource protocol and implement the cellForRow method ? (in TableView i can skip the cellForRow when using static cells).
Thanks for the help.
From the official document:
Every collection view must have a data source object. The data source
object is the content that your app displays. It could be an object
from your app’s data model, or it could be the view controller that
manages the collection view. The only requirement of the data source
is that it must be able to provide information that the collection
view needs, such as how many items there are and which views to use
when displaying those items.

Resources