How to get indexPath of UICollectionViewCell based on cell text? - ios

I pass the cell title label from "collection view A" to "collection view B". So now in collection View B, I have the cell's title label.
In the viewDidAppear method for collection View B, I'd like to use scrollToItemAtIndexPath to scroll to the proper index containing the cell with that title label.
How would I go about doing that?

Collections work like TableViews. Most people have a backing store of data behind their collection or tableview, which is an array with data in the proper order. Thus, when your cellForRowAtIndexPath or cellForItemAtIndexPath call is invoked, you simply stuff in the value from your backing store. If your model is the same as this you would simply search your collection view B backing store array for the label, and make note of what row it is in.

Related

how to send data from uitableviewcontroller to uitableviewcell

I have the following situation
A TableViewController (ExploreViewController) has custom cells - CategoryTableViewCell. Each cell has a collection view with UICollectionViewCells.
I would like the CategoryTableViewCell to contact the ExploreViewController when displaying the collection view cells to determine what data it needs to display. The data is dependent on the index path row of the table view cell and on the index of the collection view cell.
How can I accomplish this using delegates?
I wouldn't do it that way. I would pass the data each cell needs to it as a model object in the table view controller's cellForRow(at:) method. If a cell's data changes, pass it a new model object. Then the cell can use that model data to respond to data source calls from its child collection view.
If you are determined to use the delegate pattern, or something like it:
Let's call it a data source instead of a delegate. Design a CategoryTableViewCellDataSource protocol. When you create a CategoryTableViewCell, set the table view controller as its data source.
Have the cell ask it's data source for the data to display. The delegate protocol would use the cell's position to figure out it's index path, and then fetch the data from the model and return it. (You'd find the midpoint of the cell's content view, convert the point to the table view's coordinate system, and then use the table view's indexPathForRow(at:) method to figure out the index path.

UICollectionView showing selected cells

i have a tableview with form fields, and there is a row that use segue to call an UICollectionView, everything is works except that i can't keep the selected cells (the visual effect that i show when the cell is selected) after go back to the tableview.
I mean, i selected my UICollectionView cells, after that i go back to the form, but if i need to again to my UICollectionView Cells, to deselect o select more cells before submit the data, once the UICollectionView appear my previous selection are there (i print the array, and i see the values) but i cant see the effect that i did for selected cells.
How i can keep the effect for selected cells if I'm going back to select again o deselect cells?
One thing to realize is that this is not going to happen by itself. When you segue back to the table view and the form fields, the UICollectionView is completely destroyed. The next time you show the collection view, it is a new and different collection view.
So, if you want to maintain a knowledge of what the selection was in the collection view, you are going to have to maintain it yourself, deliberately, storing it somewhere when you know that the collection view is being destroyed.
That way, the next time you show your collection view, even though that will be a completely new and different collection view, you can pass the knowledge of what the selection was to that collection view as you create and show it. The user will have the illusion of "returning" to the collection view, in the same state, but in fact you will have saved and restored its state.
It is then just a matter of reflecting the selected state in the collection view's display of its items. To do that, you need to configure your model so that when cellForItemAtIndexPath: is called, each cell looks selected if that item is supposed to be selected.
Every time you use segue to call an UICollectionView a new instance of UICollectionView is created and hence states of selected cells doesn't get restored.
I believe u must have taken an array of index paths of selected indexes to show the visual changes in the cell.
Make your tableView class as delegate of UICollectionView. Using its delegate methods return the selected index array to tableView class .And before pushing to UICollectionView send the same array of index paths back to the UICollectionView. Hope it helps.. Happy Coding.. :)

UITextField values inside UICollectionView cell in swift

I have UITextField and an ImageView inside UICollectionView cell. It loads data from web service. The user can input data inside the text field and have to submit it. My problem is, when I scroll the collection view, the entered value in one text field in a cell got messed up with other text field values from another cell, and it displays wrong values.
my steps(suppose I have cells A, B, ......., k)
1.entering values
Cell A Textfield= 12
Cell B Textfield= 13
2.scrolling down
(I haven't entered anything in cell F Textfield, though it shows 13)
3.scrolling back to where I entered values
Cell A Textfield= 12
Cell B Textfield= (blank)
You can solve this problem by keeping the values of UITextField in an Array. Whenever you are entering value to UITextField and dismissing keyboard,then save that value to array at the same cell index value in array and when you scroll your collectionView, the textfield value should entered from array and it won't misplace value.
UICollectionView and UITableView reuse a handful of cell objects as you scroll. This helps with memory management. It also means that when you populate one text field on a cell, that text field's cell will get reused for a different row as you scroll, so if you don't reset the text field's content, you'll see the same data showing up for the wrong rows.
The solution is that your "source of truth" (aka model) must always be separate from your UI. So when the user types something in a text field, your model (perhaps an array of strings?) should be updated accordingly. Then when the user scrolls, you must use tableView:cellForRowAtIndexPath: or collectionView:cellForItemAtIndexPath: methods to populate the cell with the appropriate data. If there is no data, you must clear any data that may have been left over from another row whose cell got reused.
Other opportunities you'll have to deal with cell reuse include the willDisplayCellForRowAtIndexPath method on UITableViewDelegate, or prepareForReuse on UITableViewCell. Collection view objects have corresponding methods as well.
As far as I can understand from question I think the problem is, you are not updating values every time - cellForRowAtIndexPath function gets called on scrolling.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
what you need to do to resolve this issue is "You have to store all the values you have entered in your cell's textField and whenever above datasource method gets called for UITableView, updated cell accordingly".

UITableView carry cache instead of visible cell

I have a table view displaying few table cells, eg: A, B, C.
I also have a button will filter certain cell and display some other cell.
Lets say all the cell will use the same instance, although it have been filter or reappear in the table view. It will not initialize again.
The problem is after i click on filter button, the visible cell become A,D,C.
when i click on the button on cell "D", the information displayed is belong to "B" which suppose filtered.
I tried [table reloadData]; but it still displaying what suppose filtered.
Remarks: The ways i filter is removeAllObject in the tableview, and verify each cell is valid to display on tableview and add each into the tableview again, instead of insert and remove.
Hi after you have fliltered your cell you need to change datasource as well and reload tableView. You have not changed dataSource so table still had A,B,C . That's why it's showing information related to B instead of B

iOS: Adding row to tableview

I have a tableview that is based on a array of DB results, these results contains a date field. I have a custom cell that contains an image and labels. I'm trying to do:
At cellForRowAtIndexPath I verify if the date of current item (objectAtIndex:indexPath.row) has date field bigger than the last item (objectAtIndex:indexPath.row-1). If this is true: I want to add a cell filling the ImageView with a certain image, but I need to add a new row just for show this image.
How can I do this? I'm already doing this verification, but I need to add a new cell...
Do not use the cellForRowAtIndexPath to decide how many cells you want to have. At the point this method is called you should have already setup the data source to provide table view with all information needed.
Here is what you need to do. Refactor your code in a way so you:
Setup the data source first.
Force reload of the table view either by calling the reloadData method.
hey you can add the object in your data base(for example ns array) and refresh the table view with method
[tableView reloadData];
then the method cell for row at index path will be called again and it will refresh the table view's items.just make sure the method cellforrawantindexpath in your code knows to handle the new data type(make validations).
Your tableView data source should not contain any of that logic where the content of once cell depends on the content of another cell. Instead, you should have a data item for each requested indexPath and that data item should contain ALL logic necessary for the cell to be configured. If an action on that cell has an effect on how another cell should look, you apply a change to the corresponding data-item, and then call reloadRowsAtIndexPaths: for the indexPaths.
In short: configure cells ONLY in tableView:cellForRowAtIndexPath: or tableView:willDisplayCellAtIndexPath, and ONLY do configuring. Other logic should be placed in some data(-controller) object.
As suggested, you should add an item to your data-array. Then call -insertRowAtIndexPath: on the tableView. ReloadData is like a big ugly hammer that you only use when ALL of the data for that tableView changes.

Resources