How to get all the UITableViewCell from UITableView? - iphone

I have a list reusable UItableViewCells. As what I want: after selecting one of the cells, the app should update the images on each Cell.
For solving this requirement, I use method visibleCells to fetch all the cells from UITableView, and update the images in each cell by iterating the returned cell array.
But the problem is: I can not get all the cells, method visibelCells only returns the cells currently appear on the iPhone screen.
So, any idea about how to get all the reusable cells into one array?

I think that you might be misunderstanding how the UITableView is implemented. The important point to understand is that cells that are not visible i.e don't actually exist (or at least they might not)
Accordingly, you should update the images on the visible cells and update the images on other cells as they become visible in your implementation of
-(UITableViewCell) cellForRowAtIndexPath (NSIndexPath *)indexPath

Related

How can I replace `UITableViewCell` with a `UIScrollView` or `UITableView` after the main `UITableView` is already loaded?

So I am messing around with how this feels in my app. I have a UITableView and I have the swiping/panning in place, where I kind of flick the cell away and once the cell is away I have it call a method in the same class where I have my UITableView as a property. Once that cell is swiped away and that method gets called, I want to animate the size of the now empty cell to be about 3 times the size of the cell and have a UIScrollView or UITableView be added. It will be a place to store all comments about that cell.
Basically, I would just like some ideas on how I can replace a swiped cell with a UIScrollView or UITableView and animate it to three times the size of any cell.
In a nutshell, if the text below is tl;dr:
call reloadRowsAtIndexPaths, remember which cell you've changed, update cellForRowAtIndexPath and heightForRowAtIndexPath accordingly, and you are set to go.
Full version:
When you said swiped away, did you meant that you are reloading or deleting certain row of your UITableView?
If so, I think you might be able to do what you described by calling [tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationLeft]; (or whatever direction you'd like), then saving the indexPaths you've changed (in your case only one indexPath), and react appropriately in the cellForRowAtIndexPath dataSource method - changing the normal cell for another one, custom-made, with UITableView or UIScrollView inside, and also reacting in the heightForRowAtIndexPath delegate method, to change the height.
The animation itself will be handled by tableView, so that's an ease for you.
Also, if you will use this method to change the contents of the cell for UITableView, don't forget to assign delegate and dataSource of that cell's new tableView, since that causes lot of troubles when forgot.

CellForRowAtIndexPath returning the wrong cell

I am running into an issue where cellForRowAtIndexPath is returning the wrong cell and therefore, it is seguing and performing other actions on the wrong cell.
I have built custom cells that are quite big because I am fitting some UIImageViews (+text +buttons), so a cell takes up the entire screen. When you scroll down and two cells are on the screen at the same time, if you tap on the top one, it segues to the content of the bottom one (same for button actions inside the cell).
I thought this might be specific to IB, so I rebuilt everything in code, but am still having the same behavior.
Is there any way to fix this and maintain the correct indexPaths?
I think you have your perspective wrong. cellForRowAtIndexPath doesn't return wrong cells. It should just return a fresh cell, which may have been recycled if you're using dequeueReusableCellWithIdentifier or similar. In which case, dequeueReusableCellWithIdentifier will return any old cell which may have just scrolled off the top. It's your job to take the fresh cell (which may be a new cell or a recycled one) and stuff the correct values in it, inside cellForRowAtIndexPath. Then the cell it returns will be the "correct" one because you've just written all the correct values to it. So I would look at your implementation of cellForRowAtIndexPath to make sure that correct values for the index path that you're passed are stuffed into the cell that cellForRowAtIndexPath returns.

UITableView Selection Addition Problem

Having this problem that needs to be adressed unfortunately....
I am adding a custom UIButton to my UITableView Cell when it is selected however as I scroll down into the TableView I find that multiple cells are getting the buttons attached:
Here is my code:
http://pastie.org/1309795
What am I doing wrong?!
You're being bitten by cell reuse. You need to learn how to sanitize your cells when you get a cell that has been recycled.

iPhone: Setup static content of UITableView

"A Closer Look at Table-View Cells" from Apple, explains how to use "The Technique for Static Row Content" to use Interface Builder to setup the cells in a tableview.
I have some cells with different heights in my tableview. Without using the heightForRowAtIndexPath method everything get messed up. Do I still need to use this method or can I setup the height of the cells inside the IB as I created them there?
Also when using the "The Technique for Static Row Content" from the guide you still need to use the cellForRowAtIndexPath to setup the cells even if they are created in IB. I would like to setup the full layout of the tableview with all cells in IB (drag the cells right into the tableview), is that possible in some way?
While it would be nice if Interface Builder could do all that, it doesn't. To make things look correct, you have to implement the heightForRowAtIndexPath: and cellForRowAtIndexPath:. To make things easier for yourself, you could make an array and just look these things up (assuming you don't have too many rows in the table).
Yes, you have to implement heightForRowAtIndexPath.
However, in case you have static rows only, you don't have to implement cellForRowAtIndexPath, because static cells are generated by IB.
To get cell and its content (which is probably required to calculate height, eg, labels on different languages can have different length, resulting in a different cell size, etc), you can use ancestor method, like that:
UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];

IPhone - Get reference to cells that are currently on screen

I have a Table View Controller with cells. I want to update the text that is displayed on the visible UITableViewCells. Is there a way to get a reference to the cells that are currently on the screen?
You can query the visible cells;
[myTableOutlet visibleCells];
Where myTableOutlet refers to your UITableView, and visibleCells returns an array of table cells. You could also use indexPathsForVisibleRows, which will return an array of index paths for the rows that are currently visible.
If you put the code in a ,notification triggered, function, you could update only the currently visible cells.
What you can do is go and ask for the cells at index paths using - (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath, if they are not nil, you know they are on the screen and you can do what you wish with them. Also if you know the visible rectangle on the screen (which shouldnt be hard to get) you can use tableViews - (NSArray *)indexPathsForRowsInRect:(CGRect)rect and you can get all your visible index paths and use cellForRow in order to get the cells back.
As long as this is marked as answer, I stand corrected, like others said you can do [tableView visibleCells]
Call the reloadData method of the UITableView. It will force the table view to update all the visible cells information (check the documentation for more information).