I'm creating a function to keep track on deadlines. When you select a row in the deadline table view I change the accessoryType to Checkmark. This works perfectly with this code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Cell *selectedCell = (Cell *)[tableView cellForRowAtIndexPath: indexPath];
if (selectedCell.accessoryType == UITableViewCellAccessoryNone) {
[Deadline setDone: TRUE onIndex:indexPath.row];
selectedCell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
[Deadline setDone: FALSE onIndex:indexPath.row];
selectedCell.accessoryType = UITableViewCellAccessoryNone;
}
[tableView deselectRowAtIndexPath:indexPath animated: YES];
}
The problem occurs when you have selected a table cell and the scrolled so it dissappears, when it appears again the accessoryType is None again.
My code for deciding accessoryType in cellForRowAtIndexPath is:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CellIdentifier";
Deadline *d = [self.arrDeadlines objectAtIndex:indexPath.row];
Cell *currCell = (Cell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
开发者_StackOverflow
if (currCell == nil)
{
UIViewController *c = [[UIViewController alloc] initWithNibName:@"Cell" bundle:nil];
currCell = (Cell *)c.view;
[c release];
}
currCell.lblTitle.text = d.name;
currCell.accessoryType = d.done == TRUE ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
return currCell;
}
How do I solve this?
You should not use a TableViewCell to keep data. This is where your dataSource is for, the TableViewCell is just a visible representation of the data in your dataSource.
Since cell are reused after they are no longer displayed all data gets reset.
Thus in your UItableViewDataSoucre you should check whether a task is done and set the accessoryType yo the checkmark.
So add an NSLog in the cellForIndexPath method and check whether the done boolean is really set.
精彩评论