Stop thread in UITableViewCell before it is released - iphone

I have this kind of problem where I want to download some images that will be used as thumbnails for each cell.
In order to achieve this behavior, there is a thread working in each UITableViewCell that have the role of downloading and saving the image.
But I want to stop the thread from working if the cell is not visible anymore.
Is there some delegate methods for UITableViewCell like viewWillDisappear that will help me solve this issue?
If I am thinking wrong, Is there any other way that will help me achieve this same behavior?
Thank you for your help.

UITableViewCell is only for rendering something to the screen. The controller that is controlling your tableview should be the one downloading the thumbnails and reload its data once it has the thumbnails.
A tableview only has 1 cell, it copies it in order to display the multiple rows.
Use the delegate method "willDisplayCell" to set the data of your custom tableview cell.

Due the reusable cell mechanism a single instante of UITableViewCell can be reused for display different data so UITableViewCell is not meant to be used for this logic but only for presentation purpose.
I suggest you to rethink your app logic

Related

iPad UITableView flickering issue

I have a UITableView that contain about 20 rows in the iPad's viewport. I have a interval timer that will call UITableView's reloadData regularly(post it to UI thread).
Now when I scroll through the UITableView with medium speed (not so fast), that UITableView will refresh with flickering effect.
I have to write a function to manually update the UITableViewCell label by looping through all the items in the array (this array keep all the items that show on UITableView). I will execute this function when timer is running instead of calling reloadData (as I mentioned above). Then the flickering issue is gone.
I believe that reloadData should be better than looping through all the data because reloadData will only refresh the current showing Cells instead of all the rows, but I couldn't figure out why the flickering happens. Anyone know why?
One thing I have to mention is I did use the CellIdentifier correctly to reuse the cell and only create the cell when the retrieved cell is null.
Moreover, I do not have this issue in iPhone and I believe that it is because iPhone has lesser row compare to iPad.
Anyone can give some explanation about this issue?
I had the same problem with flickering when using reloadData. I solved it by using indexPathsForVisibleRows and cellForRowAtIndexPath: to only update the visible cells. Performance is good since I don't have to iterate over the whole data set, but only a limited number of visible cells.
reloadData causes the tableview to recreate the visible cells on screen which can result in a flickering since the cells get destroyed. There are better ways to reload the tableview. Are you using Core Data? If so the NSFetchedResultsController and it's delegate are a great way to update a tableview since it listens to changes in the underlying datasource and only updates the appropriate cells.

Calling UITableView's cellForRowAtIndexPath: for just one iteration

When attempting to create a form with UITextFields, it appears that cellForRowAtIndexPath: gets called every time a user scrolls up and down the tableView. When this happens, a new UITextField is created, and the old UITextFields are no longer visible. Is there a way for the cellForRowAtIndexPath: method to be called for just one iteration?
Check the docs for UITableViewCell, especially A Closer Look at Table-View Cells and the section about static cell content.
Perhaps you might consider a simple UIScrollView rather than a UITableView? It's difficult to tell what you're trying to accomplish here. Perhaps add a bit more detail to your question.
Create UITableViewCell nibs in Interface Builder with UITextFields and link all of the objects with the viewController class.

reusing elements in tableview cell

I am adding a textfield to each row in tableview and releasing it after adding it to the cell.
I want to reuse when UIButton is clicked.
I tried some methods but it does n't worked.
Help me how to reuse it.
Try to do an custom cell view. Subclass the UITableViewCell and use the normal reuse of cells, that is the best way to do it. Improves scrolls on slow devices like iPhone 3G.
Take a look here.

Threaded thumbnail-loading for imageview in a table cell

I'm looking for suggestions on how to make an efficient thumbnail-loader for navigation-lists in the form of tables. I can start a thread and download and cache the thumbnails, but I'm unsure on how to update a table cell with the image (a cell either visible or outside the view).
I think the app Blocket does what I'm after, if I remember correctly.
I'd like to set a placeholder image, and as the thread loads each image, it updates the cells. The placeholder could be an animated activity indicator (spinner), if possible.
Are there any terse example-code out there, or can you give suggestions on how to communicate the update to the cell and force it to display the image immediately?
Interesting problem.
I guess at some level you'll need to replace the existing cells in the table view via the reloadRowsAtIndexPaths:withRowAnimation: UITableView method, so I suspect you'll need to keep a lookup of cell contents -> indexPaths so you know which cells to update when the relevant asset becomes available.
However, you'll need to avoid breaking the existing cell re-use system. (i.e.: there's no value in pre-creating all of the cells in advance.) As such, much of this work should be done within the - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath UITableViewDelegate method, I suspect.
That said, hopefully you'll get a better response than these somewhat vague ramblings. :-)
Made the nicest threaded thumbnail loading I've seen yet on iPhone :D The hardest part was cancelling a thread when navigating away from its table. An NSThread loading images and performSelectorOnMainThread waitUntilDone:YES for when updating the cell.imageView, passing objects (cell, tableView, indexPath etc) in an array to bypass the limitation of passing only one object in withObject. Looks royale with a quick fade-in of each image as they load :)

Load a nib into an UIView?

I need some help.
I've got a segmented control. if segment 0 is clicked it shows a UIView, containing a uitableview.
If segment 2 is clicked it shows another UIView which contains a uitableview as well.
These two tableviews got the same datasource. But i want the second tableview to have an other datasource. SO i thougth of loading another nib into the second uiview containing a the tableview which gets its data from the corresponding .m file.
I don't know wheather this is the best solution. If you've got any other ideas let me know :)
thanks in advance!
I think the best way to do this is to have just one table view. In the cellForRow method have a conditional which queries the state of the segmented control. Depending on the result of that if statement, return the type of cell you want. In addition, in your segmented control callback you'll have to do a [tableView reloadData]
Do you know that you can have the same tableview display different data sets? You don' have to use an entirely different NIB just to change UITableViews so you can display different data. Or am I understanding your question incorrectly?