Reputation: 170
As we know, UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]];
method is invalid after the cell is out of our sight. (it's been put to the pool of reusable cells by system) So how do we track the information inside this cell?
I created another cell variable to point to it.In method -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
I make _trackMarkCell = cell;
trackMarkCell is a property of current controller. This way I can always get the information of the cell. But I feel this way is not that smart, is there any better solution?
Thanks.
Upvotes: 0
Views: 45
Reputation: 2084
What you can do is, you can get data from the cell and put in your collection in UITableViewCell's prepareForReuse
method: this will be called before the cell is reused.
Upvotes: 0
Reputation: 8402
So, basically , UITableviewCell
is just a view, that is used to display the content in UITableView
so from the method -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
you can crate a new cell or if the cell is already in the pool you can reuse it
come to your question, with the information i mentioned above, the cell holds the information that is used to display on tableview so u just no need to worry about the cell, but the data what you are displayed in the cell. u just keep track of the data and while updating the cell, clear all the information of the cell (which is crated newly or reused from the pool) before using it. for example
lets say suppose u are displaying the image in the cell, but after some time, image in the cell which is changed, in this case u just change the image which is present the datasource not in the cell, if u are using the images array to display the images in the cell,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kMenuTableCellReuseId];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kMenuTableCellReuseId];
}
//hear clear all the info present in the cell
//no need of this also u can directly update the cell, but for clarity i mentioned this
cell.myImageView.image = nil;
//just load the cell with new data
cell.myImageView.image = [dataSource objectAtIndex:indexPath.row];
//.... other code
return cell;
}
one more thing using the another cell to keep track of the cell which is present in the pool which might give u different information of other cell. or that cell might be deallocate. so just keep track of the data not the table view cell.
Upvotes: 1
Reputation: 8835
So how do we track the information inside this cell?
You just don't. Cells don't store information. Cells display information from a data source. You store the data in arrays or Core Data or on a server or anywhere else. Then you just display it in the cells.
Upvotes: 0
Reputation: 91
Not sure how you're populating your cells, but if you're feeding the cells via an array of model objects, you'd have access to all your data within that array. You could reference the object with the information you're seeking based on the index within the array (eg. - array[3]
, where '3' is the index of the object desired)
Upvotes: 0