I have a TableView which has 2 sections and the 2nd section is only shown when the 3rd row in the first section is selected. Only the first section has images. Everything is working fine. The 3rd row in the 2nd section shows a TextField and a button when selected and you can save a date in there via pressing the finish button. Works also fine. You can select that row and go to other rows and everything is normal. If though, you tap into the TextField and don’t save the input or don’t write anything, then it bugs.
It will reload the table with images at random rows it seems.
I thought it’s the keyboard which pops up, so I tried to manually dismiss it. Bug is still there.
I am really curious why this happens, so I am hoping for help! Thanks in advance!!
My cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
BOOL tooltipValue = [[NSUserDefaults standardUserDefaults] boolForKey:@"tooltipSetting"];
BOOL compactSettingValue = [[NSUserDefaults standardUserDefaults] boolForKey:@"compactModeSetting"];
NSDictionary *dictionary = [self.settingsArray objectAtIndex:indexPath.section];
NSArray *array = [dictionary objectForKey:@"Items"];
NSString *cellValue = [array objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
if (indexPath.section == 0 && self.iconSetCount < 3) {
NSDictionary *imagedictionary = [self.imagesArray objectAtIndex:indexPath.section];
UIImage *cellImage = [[imagedictionary objectForKey:@"images"] objectAtIndex:indexPath.row];
cell.imageView.image = cellImage;
self.iconSetCount++;
NSLog(@"%d",self.iconSetCount);
}
if (indexPath.section == 0){
if (indexPath.row == 0) {
if (tooltipValue){
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
if (indexPath.row == 1) {
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
if (indexPath.row == 2) {
if (!compactSettingValue){
cell.accessoryType = UITableViewCellAccessoryNone;
} else {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}
} else if (indexPath.section == 1){
// if the sellected row is the one with a custom date
if (indexPath.row == 2 && indexPath.row == isSelected) {
// if an archive exists for the custom date then get the data from the archive
if ([[NSFileManager defaultManager] fileExistsAtPath:[self getDocumentPathForCustomDate]]) {
NSString *customDate = [NSKeyedUnarchiver unarchiveObjectWithFile:[self getDocumentPathForCustomDate]];
cell.textLabel.text = customDate;
} else{
//show a label with a done button where you can type in your custom date and save it
if (self.textField == nil) {
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(5, 5, 200, 35)];
self.textField.backgroundColor = [UIColor whiteColor];
self.textField.textColor = [UIColor blackColor];
self.textField.font = [UIFont systemFontOfSize:16.0];
self.textField.borderStyle = UITextBorderStyleRoundedRect;
}
if (self.button == nil) {
self.button =[UIButton buttonWithType:UIButtonTypeRoundedRect];
self.button.frame = CGRectMake(210, 5, 50, 35);
[self.button setTitle:NSLocalizedString(@"done", @"") forState:UIControlStateNormal];
[self.button addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
}
[cell.contentView addSubview: self.textField];
[cell.contentView addSubview: self.button];
}
}
if ((indexPath.row == self.isSelected)){
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}else{
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
return cell;
}
My didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
/*
DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"Nib name" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
*/
BOOL tooltipValue = [[NSUserDefaults standardUserDefaults] boolForKey:@"tooltipSetting"];
BOOL compactSettingValue = [[NSUserDefaults standardUserDefaults] boolForKey:@"compactModeSetting"];
if (indexPath.section == 0) {
//tooltips
if (indexPath.row == 0) {
if (!tooltipValue){
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"tooltipSetting"];
} else {
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"tooltipSetting"];
}
[self.tableView reloadData];
}
//go to Set your Signature Extra
if (indexPath.row == 1) {
AdditionalSignature *additionalSigi = [[AdditionalSignature alloc] initWithNibName:@"additionalSignature" bundle:nil];
[self.navigationController pushViewController:additionalSigi animated:YES];
[additionalSigi release];
}
//compact version
if (indexPath.row == 2) {
if (!compactSettingValue){
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"compactModeSetting"];
} else {
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"compactModeSetting"];
}
[self.tableView reloadData];
}
}
// if in optional compactmode settings
if (indexPath.section == 1){
// and "none" is selected remove first the custom date
if (indexPath.row == 0) {
[self removeCustomDateArchive];
//then change the userdefault for this setting
if (![self isOptionNoDate]) {
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"dateSetting"];
}else{
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"dateSetting"];
}
}
if ( indexPath.row == 1) {
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"dateSetting"];
[self removeCustomDateArchive];
}
if (indexPath.row == 2) {
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"dateSetting"];
}
self.isSelected = indexPath.row;
[self.tableView reloadData];
}
}
The cell identifier is abused here. Removing the cell identifier would be the quickest solution here, reallocating the cells every time.
Rewriting the code to include the cell identifier would make it more robust.