Reputation: 668
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *reuseIdentifier = @"cellReuse";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
}
NSManagedObject *manageObject = [self.fetchResultController objectAtIndexPath:indexPath];
cell.textLabel.text = [manageObject valueForKey:@"title"];
cell.detailTextLabel.text = [manageObject valueForKey:@"subtitle"];
return cell;
}
I've read from a book that cell is a kind of "reuseable view". I have some problems in understanding the "reusable".
Could anybody explain in an easy way about the mechanism of cell?
Upvotes: 3
Views: 1531
Reputation: 374
Some facts
UITableViewCell
.UITableView
only put cells in the reusable queue when they go outside the visual window.Once you start scrolling the table is when UITableView starts to put UITableViewCells
in the reusable queue and can be reused in other positions of the table.
Why this behaviour is not explained in dequeueReusableCellWithIdentifier reference is really amazing. Probably because it’s evident… this entry is dedicated to those who are as dim as me.
Upvotes: 7
Reputation: 10172
In addition to H2CO3, As you can see you have used a variable named reuseIdentifier
and you are using following lines:
static NSString *reuseIdentifier = @"cellReuse";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
}
So lets go line by line:
first line specify a identifier that is static, so that it will only be constructed once. If it's not static, you'll be making one every time the message is sent (which is a lot), as Lou Franco said.
Then come's importent line, the second one, that contain dequeueReusableCellWithIdentifier
. This method load dequeue the cell that is no more visiblem just like doubly linked list that assure the reusability of cell which are not in view, (that means only those cell remain in memory which are visible to you, that's the reusibility).
Now as you can see what 'if block' do is, if there's no cell to dequeue, then it creates new cell with the reuseIdentifier
so it can be reuse when it's not visible any more.
In the end you don't have to bother that which cell is eligible for reusing, tableView class calculate it with respect to tableView frame size and cell height.
Upvotes: 1
Reputation: 42977
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellID=@"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
cell.textLabel.text= [NSString stringWithFormat:@"%d", indexPath.row];
}
else
{
cell.textLabel.text = @"reused";
}
return cell;
}
To understand the cell reusability run the above code. suppose we have 50 rows return 50 from cellForRow. We can see that table view is creating only 11 cells(0-15). When you scroll up/down tableView is reusing the already created cells. This reusable mechanism of table view saves a lot of memory. It wont create the cells un unnecessary, always reuse the non visible cells
Upvotes: 1
Reputation:
The problem: a table view can potentially have thousands of rows (or millions, whatever). It would be tedious and wasteful to create a separate cell for each data row. Instead, the table view only asks for as many rows as it displays on the screen at the same time (this is typically no more than 10-15-20 cells). This is manageable, doesn't consume a lot of memory and plays nice with the fact that not all of the rows are visible on the display anyway.
So, when the table view needs a new cell to be displayed (because the user has scrolled the view), it takes a cell that went out of the visible area, and queues it back to the end, reusing it.
Upvotes: 13