19

I found this method called - (void)prepareForReuse. I read the document but I don't understand how to use it.

When I scroll my tableview, it gets stuck, or has slow scrolling and I intend to use prepareForReuse. Can someone please point me to a good tutorial or give me some sample code so i could learn.

Sorry I don't have any code to demonstrate my working.

Cœur
  • 37,241
  • 25
  • 195
  • 267
shajem
  • 2,111
  • 5
  • 22
  • 30
  • 1
    I doubt that `prepareForReuse:` will solve your performance problems... – JustSid Feb 20 '12 at 14:28
  • To clear the `UITableViewCell` from any content that you might want to reset before it appears again in the `UITableView`. Its only needed if you write your own subclasses with own controls and you should never invoke it directly. – JustSid Feb 20 '12 at 14:32

2 Answers2

19

The prepareForReuse method is called by the owning table view on a cell that is not needed right now. You are supposed to do minor cleanup here for your custom cell, like resetting alpha values and the like, so it can then later be reused. You shouldn't call it yourself, BTW.

You only ever need to care about it if you're implementing custom table view cells: you overwrite it to do your cleanup.

You're "using" it simply by using reuse identifiers in tableView:cellForIndexPath::

static NSString *CellIdentifier = @"Identifier";

cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                   reuseIdentifier:CellIdentifier];
    [cell autorelease];
    // Set up your cell
}

To further improve your table view performance, see the question Tricks for improving iPhone UITableView scrolling performance?

Community
  • 1
  • 1
DarkDust
  • 90,870
  • 19
  • 190
  • 224
  • "You shouldn't call it yourself, BTW." Why do you think so? Didn't find anything about calling it yourself in Apple docs. What if I want to perform some additional cleanup when the cell has just left the screen? – Legonaftik Oct 25 '18 at 15:52
  • @Legonaftik: There is simply no need to. The table view already calls the method at an appropriate time and it's a waste of cycles to call it yourself. If you really _have_ to clean up resources as soon as the cell not being not being displayed any more, you could consider overwriting `didMoveToWindow` of your cell and check whether `self.window` is `nil` to detect the view being taken off-screen. – DarkDust Oct 26 '18 at 06:43
  • I think it depends on a specific case. If you want this cleanup behaviour to work on every single View Controller where you reuse this `UITableViewCell` subclass then it's fine to override `didMoveToWindow` of the cell. However if this cleanup behaviour (cleanup when out of the screen) is specific for a single View Controller then it's better to override `tableView:didEndDisplayingCell:forRowAtIndexPath:` method of `UITableViewDelete` and call `cell.prepareForReuse()` in it. – Legonaftik Oct 26 '18 at 08:28
  • @Legonaftik: You can do that. But I still stand by my initial words: you _should_ not call it yourself as _usually_ there is no need to and you're doing unnecessary work if you do. If you know what you're doing and you _really_ need to do some cleanup as soon as the cell is taken off-screen, you may call it, of course (or implement a new method that is called instead, or do any other scheme that fits your specific need). – DarkDust Oct 26 '18 at 08:35
14

You use it if you have logic in your custom cell class that may or may not modify a cell property. For example if your table calls your cell, but in some cases it can modify the background colour, but some cases it won't and would use the default you've set, then you would need to implement

-(void)prepareForReuse{
    [super prepareForReuse];

    // Then Reset here back to default values that you want.
}

Otherwise, it could reuse the previous values you've set if your logic does not change it.

Kerollmops
  • 303
  • 4
  • 15
mskw
  • 10,063
  • 9
  • 42
  • 64
  • 2
    How do I set it so that the cell that was modified stays modified while the other cells that will be "reused" stay as the default option? – Lukesivi Nov 25 '15 at 15:31
  • @lukesIvi Were you able to figure out the solution to your question? – dnadri Feb 24 '16 at 05:17