I made a UICollectionView, and everything is working. It makes 100 cells that I can scroll through in simulator with no problem.
However, rather than seeing all the cells at once, I want the cells to be released one by one whenever that red button is pressed.
I am confused because I noticed in the storyboard, it hard codes the number of cells it has on the screen at once. Is there any way to get around this?
Thank you!
This is what the UI looks like in storyboard.
This is the code I used to make it. It's basic, and just says to fill the text box of the cell with a string from the array.
Your question is garbled.
A collection view has a delegate and a data source. The data source responds to messages in the UICollectionViewDataSource protocol. That protocol lets the collection view ask how many sections it has, and how many rows in each section, as well as asking for the cells from those sections and rows.
There are also methods that let you tell the table view that you want to add more cells. Take a look at the method insertItems(at:). That lets you provide an array of indexPaths, which tells the table view that you have added new entries.
You could certainly write a button action method that added one or more entries to your data model and then used the insertItems(at:) method to notify the collection view that it had new entries. If there was room in the content view of the collection view to display additional cells it would then call the data source and ask for new cells at those index paths.
Sounds like you just need to keep track of how many items you want displayed (which will increase the more that button is pressed) and use that in your UICollectionViewDataSource method. Something like:
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return min(myRunningItemCount, maximumNumberOfItems) //assuming there's a maximum
}
Then you just need to call reloadData on the collection view whenever that number changes.
Related
I am working on a existing project.
The project has one screen where details of an orders are displayed.
Every order has order items. So, naturally a table view is used to display order items. This works fine.
Now, the system has been extended in that way that every order item can have 0 or more toppings. If there are toppings for order item, they also need to shown in a list.
So, the approach I took is to try and implement a table view which will be responsible for showing order item toppings inside order item cell.
I've read about this approach and people say that it is possible.
The problem I have now is that for toppings table view in order item table view cell, the numberOfRowsInSection method is called, however the cellForRowAtIndexPath is not called at all.
I have read about this issue and the people say that is has to do with table view frame not being set correctly.
I've setup the order item with toppings cell in Interface Builder with AutoLayout.
I (think) all the constraints are in place in order for cell to render it's content and height correctly.
However, from the screenshots it can be seen that the toppings tableview is not rendered at all.
Anybody has an idea of what might cause the table with not to render?
Any help, thoughts, comments are appreciated. Thank you.
The problem seems to with your UITableViewDelegate and the way you want to achieve automatic height for your cells.
In heightForRowAtIndexPath you return UITableViewAutomaticDimension, which actually is equal to -1.0. If you look at that function's description in documentation you will see
Return Value
A nonnegative floating-point value that specifies the height (in points) that row should be.
Now, if each of your rows have negative height, then the table view has nothing to display - hence cellForRowAtIndexPath is never called.
TL,DR
You were almost there - UITableViewAutomaticDimension should be set to UITableView.rowHeight, not returned in heightForRowAtIndexPath
toppingsTableView.rowHeight = UITableViewAutomaticDimension
Also, remove the functions heightForRowAtIndexPath and estimatedHeightForRowAtIndexPath
This question already has answers here:
Calling setCollectionViewLayout:animated does not reload UICollectionView
(7 answers)
Closed 6 years ago.
I have a UICollectionView that is using grid layout currently I have two cells in a row.
I want to give the user a choice to switch between grid and list layout similar like table view.
How can I proceed for the same.
use segmentcontroller with uicollectionviewcontroller and uitableviewcontroller. then implement delegate methods and datasource methods for both. then hide one view controller tableview or collectionview. then implement the action for segment controller.
when segment index == 0 show collectionview else show tableview. hope this will help to you.
or else you can use same uicollectionveiw with sections.
numberOfItemsInSection shoud return 0 that means there is one item(row) per section. You consider this as list view. you can manage size or look and feel accordingly.
numberOfItemsInSection returns 2 or more whatever number of items you want in your section that means it is grid view.
You have to managa datasource methods accordingly. You should have to set some flag that keep status that user have selected the list or grid.
according to that status you can manage your datasource or delegate.
Second approach :
Use collection view for grid representation and tableview for list representation and just show and hide one of them according to user's choice as a result of grid or list.
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.
I am making an interface which have UITableView with four custom UITableViewCell's. And every other UITableViewCell have also UITableView. This mean I have TableView inside a TableView.
Let me call first tableView - ParentTableView and nested tableView - ChildTableView. So I implemented method didSelectRowAtIndexPath on both tableView's. But when the app is running, only the method of the ChildTableView is being called. I need to know inside the ParentTableView, which cell is being tapped.
How can I transfer that information further from ChildTableView to ParentTableView.
This may be a silly question, but I can not find any reliable solution so far, so please help me.
Thank You in advance, kind Sir
First, I think nested table views is a bad idea. But I don't know your use case, so it might be an exception.
The table view controller class used inside a cell have its own #protocol definition and set the outer table view as its delegate. In the inner didSelectRowAtIndexPath: it can inform the outer table view about the selected indexPath, its own indexPath and any other information you might want to transmit.
Try using collection views. You can do a layout that works like a regular table view and then in the cells that need to have a table, you can make those separate cells with another collection view inside or a table view inside of it.
As mentioned before, it's not easy doing table views inside of cells, and it can be very tricky to get things to work correctly.
This is an old tutorial I wrote which may help and be of guidance. But since then there's been collection views and auto layout, so keep in mind it's very old.
In my code, some cells should not be shown to the user on the UITableView.
So my dataSource would supply objects, but I'd like to check in my cellForRowAtIndexPaht if the cell is hidden, and if it is, practically return nothing.
So that the cell's height would be zero, but more than that, I'd like to save my function the building of the whole cell.
I tried to return nil, but alas, it crashes.
The data source is supposed to return what is actually in the table.
You can easily make a new array to hold the data you want shown, and omit the data you do not want to see (keeping it in a master data source). Then as some rows in the table view are able to be seen, add them into the array of table data and use the insertRows method of UITableView to animate viewing the newly unveiled data.
Could you move the logic up into the numberOfSections or numberOfRowsInSection methods used to create your UITableView? Basically, whatever logic you are using to try and return null for those cells could move into the numberOfRowsInSection method in the UITableView class and return only the number of visible (not hidden) cells.
Then you would have to configure your cellForRowAtIndexPath method to hand out the cells from your data source in order by counting up through your visible rows instead of just pulling them out the array.
I wouldn't think you would want to return 'nothing' to a program expecting an object.
Link to Apple Reference Docs for UITableView, the numberOfSections and numberOfRowsInSections can easily be used to logically modify the amount of data being shown in a table view.