i have a problem with my UITableview. After scrolling or pushing the switch it starts sometimes mixing up the cells. So you can so the text of one cell shows up in the other. I read a lot about tableViews, but I found nothing that fits for my code.
Here it is:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
if (indexPath.section == 0) {
if (indexPath.row == 0) {
cell.textLabel.text = NSLocalizedString(@"Course:",@"Course Section");
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.detailTextLabel.text = entryTableCourseName;
}
else if (indexPath.row == 1) {
cell.textLabel.text = @"Due Date:";
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.detailTextLabel.text = entryTableDueDateString; }
}
else if (indexPath.section == 1) {
if (indexPath.row == 0) {
//cell.textLabel.text = NSLocalizedString(@"Title", @"entryTableTaskTitle");
[cell.contentView addSubview:nameField];
}
else if (indexPath.row == 1) {
[cell.contentView addSubview:TaskView];
}
}
else if (indexPath.section == 2) {
if (entryTableGradeSwitch.on) {
if (indexPath.row == 0) {
cell.textLabel.text = NSLocalizedString(@"Grading", @"GradingCell");
cell.accessoryView = entryTableGradeSwitch;
}
else if (indexPath.row == 1) {
cell.textLabel.text = NSLocalizedString(@"Options", @"GradingCellOptions");
}
}
else {
cell.textLabel.text = NSLocalizedString(@"Grading", @"GradingCell");
cell.accessoryView = entryTableGradeSwitch;
}
}
}
I feel like I’m doing something really wrong on setting up the cells.
It’s all due to cell-reusability. There are plenty of posts about this on StackOverflow, and I would recommend that you search for “UITableViewCell Reusability Problems”, and you will come across the solution.
In short .. What happens here is that when you are scrolling, the cell essentially re-uses the one used before at that indexPath, and cellForRowAtIndexPath assigns a different cell at that indexpath.
Solution ?