I have table view with multiple different cells, and one of the cells depends on property of another one. I.e. I have FirstCell, which has dynamic property count, and I have SecondCell, which contains several UITextFields. I want the number of these UITextFields to match the count property. How can I get count property from FirstCell considering that this property can be changed?
I.g. FirstCell contains count = 3, SecondCell shows three UITextFields. Property count changes to value 4 and another one UITextField appears in SecondCell.
The value of count should be in your data model. You add an observer, or put code into the setter, so when the value of count is changed, the two cells depending on it (the one displaying count, and the one showing the text fields) are both reloaded; that's reloadCellAtIndexPaths or something like that from memory.
Obviously the code that loads cells must be written correctly, and code changing count in the first cell must change the model property.
Thinks like this should be managed by the UI(Table)ViewController. The view controller should be notified when the text field changes (for example using target-action) and then update the data source and reload the relevant cells.
I have a UITableView that presents a list of nearby places. I can filter the table using a UISegmentedControl by the type of place (bar, restaurant, etc.) The issue is that when I attempt to filter the list by a segment that has 0 records, the previous segments records are not being replaced with an empty table.
Every time I click on a different segment, I'm calling tableView.reloadData() and feed the table a filtered array containing places of the selected type.
From my experience , The issue lie in the logic that you have made on dynamically change of height of tableView hopefully because only cellForRowAtIndexPath will not called in case of your number of rows are 0.It reload but some logic in cellForRowAtIndex path is there which is never called.
i try to create a UICollectionView who should open at a specific cell.
My approach is to store all cells:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) ->
UICollectionViewCell {
// my code
cells.append(cell)
return cell
}
then find the desired cell in viewDidAppear:
let result:[MyCell] = cells.filter{ $0.myProperty!.uuid == selectedUUID }
Unfortunately the desired cell can't be found within the cells array, because not all cells are stored yet.
Am i going in the right direction or do i need a different solution?
EDIT: removed misleading information
Don't work with the cell themselves.
Your cells are displaying data, right? They must. That data (could be numbers, or text, or an image, anything) should be a custom object.
For example, you're displaying a list of animals.
So you have the Animal class, with different properties (Size, weight, age). Again, all an example. Maybe you're displaying a list of students which have a FirstName, Lastname, and so on. Lets say we're using the Animal.
You have an array of Animal objects, you can have 1, or 4, or 234234, it doesn't matter. The array can hold as many animals as you want. But you only have one array.
Your collection view (or tableview) needs to create a cell for each row. How many rows?! Easy, the other methods tells us there are a specific number of rows per section. Let's say we only have one section.
How do you define rows? You give him a number. What number? The number of animals in your array. For example, 5. Or, much better, myAnimalsArray.Count. Now if you have 20 animals, you'll have 20 cells. If you have 3 animals, you'll have 3 cells.
Okay, now to the cells, you know how many you have, and the cellForRow will therefore create X cells, X being the count of your array.
You'll probably have a simple cell for testing, like
cell.title = animal.name
and then return the cell. It's all pseudo code here, but you get the idea.
Now to your actual question
I've written all that because, according to your other comments in the other answers, you seem pretty confused.
You have your animal array (the data), and no cells yet. You want to start on the animal that has a name starting with E, for example. Well, after you've all the animals on the array, you need to find the one that starts with E (or any other filter you might like). You find it with a searching method. Once its done, you will know the index of the animal in the array. It's the array item number 23, it's the Elephant.
Create an NSIndexPath object with section 0 and index 23 (because your filter found it's the 23rd item, again, i'm just using examples), and pass that indexPath object to the scroll method of the collectionview.
And you're good ;)
Do that in the ViewDidLayoutSubviews or viewDidAppear. (first one is better). But you must do it after your array of animals is loaded, otherwise you won't have any data to filter.
Once you've called the method in the link above, your collectionview will scroll, and if you've done it soon enough, it will load at that index and not at index zero.
i have solved this problem through this code hope it will helpful for you:
[self.collectionview setContentOffset:CGPointMake(0,self.collectionview.contentSize.height-self.collectionview.frame.size.height/2) animated:YES];
set above only parameter to contentsize:CGSizemake(x,y);
by adjusting your required parameter you can scroll to specific cell.
just replace above parameter inplace of c and y in contentsize:CGSizemake(x,y); method.
and you have to adjust parameter.
UICollectionView's cell is reused.
For example, if view can only show 3 cells once: A, B , C; then ur cells maybe like [A, B, C, A, B, C, A, B, ...];
each time, u set the property of cell, uuid will be reset to a new value.
so u cant filter out what u want.
Hitendra Hckr is right, u should "filter data array instead of filtering cells array".
Or u can "alloc" new cell instead of "dequeueReusableCellWithReuseIdentifier:forIndexPath:".(Not recommend)
For your case, you need to filter data array instead of filtering cells array, this way you can get exact index.
Based on the filteredIndex create indexpath using below method,
NSIndexPath(forItem: filteredIndex, inSection: yourSectionIndex)
This way you can scroll to particular cell.
Try adding a call to collectionView.reloadData() before scrollToItemAtIndexPath. This will ensure all required cells are created.
I have searched for this a lot and haven't found what I am looking for.
I want to have UITableViewCell such that each have dynamic content. For example, one cell can have a place name, address, phone number, image, etc. Another cell can have some or all of these attributes.
I know that I should have functions cellForRowAtIndexPath: and heightForRowAtIndexPath: return stuff appropriately.
The problem I am facing is that I have my cells designed in nib files, and am loading the layout from there. I can calculate the heights for each cell based on conditions(though this is tedious), also I need to arrange stuff in that cell if one thing is missing. For example, if place name is missing then move all the elements up via setFrame:.
There should be an easy way out I believe?
In such dynamic cases I would not bother with using storyboards to accomplish this. It should be very easy to do this programatically...
If you really do need to create this in a storyboard you could generate a full cell which could consist of (for instance) 5 labels. Now in the table view data source I presume you have some array of objects where each element represents a single row in the table view. If so then in your case each of this object can have a method that will return an array of non-empty strings (the strings that will actually be displayed) which can be a dynamical count from 1 up to 5 strings.
If you do this you will request this array in method requesting the height for row at index path and by using this count you can then return the height of the row (for instance numOfStrings*40.0f). Inside the method requesting your cell you can simply fill those labels with the same array you use to calculate the height.
As for non storyboard approach I suggest you to override an UITableViewCell, generate a static method that will return you the cell height for string count and generate an initializer with the string array which should then iterate through the array generating the labels and setting the text to it.
When I add objects to my datasource, I am not able to see the added objects in my collectionview.
I am able to see in the numberOfItemsForSection increment in value, but the corresponding cells do not display the objects. The indexpath.row does not exceed its original value that I set before.
HALP.