I'm using a UITableView for an application I'm building. When the UITableView goes to editing mode, the table cells display a textfield over the cell, as defined here:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellLabel = [cellDetails objectAtIndex:indexPath.row];
NSString *cellData = [movieDictionary objectForKey:cellLabel];
float x = cell.textLabel.frame.origin.x + 80;
float y = cell.textLabel.frame.origin.y + 5;
float w = cell.textLabel.frame.size.width - 80;
float h = cell.textLabel.frame.size.height - 10;
UITextField *editField = [[UITextField alloc] initWithFrame:CGRectMake(x, y, w, h)];
editField.text = [[NSString alloc] initWithFormat:@"%@", cellData];
editField.tag = EDIT_FIELD_TAG;
if (indexPath.row == 1) {
editField.keyboardType = UIKeyboardTypeNumberPad;
}
editField.delegate = self;
editField.borderStyle = UITextBorderStyleRoundedRect;
cell.editingAccessoryView = editField;
}
Then, in the setEditing
implementation:
-(void)setEditing:(BOOL)editing animated:(BOOL)animated {
NSString *msg;
if(!editing) {
for (int i = 0; i <5; i++) {
UITableViewCell *cell = [self.table cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
UITextField *tf = [cell.subviews objectAtIndex:2];
if(i==1) {
if([tf.text intValue] == 0) {
msg = @"The year you entered is not valid. Please enter a valid year.";
}
}
if(!msg) {
[movieDetails replaceObjectAtIndex:i withObject:tf.text];
[movieDictionary setObject:tf.text forKey:[cellDetails objectAtIndex:i]];
}
}
if(((NSString *)[movieDetails objectAtIndex:0]).length == 0) msg = @"The title you entered is not valid. Please enter a valid title.";
if(((NSString *)[movieDetails objectAtIndex:1]).length == 0) msg = @"The year you entered is not valid. Please enter a valid year.";
if(((NSString *)[movieDetails objectAtIndex:2]).length == 0) msg = @"The director you entered is not valid. Please enter a valid director.";
if(((NSString *)[movieDetails objectAtIndex:3]).length == 0) msg = @"The cast you entered is not valid. Please enter a valid cast.";
if(((NSString *)[movieDetails objectAtIndex:4]).length == 0) msg = @"The genre you entered is not valid. Please enter a valid genre.";
if(!msg) {
movie.movie_title = [movieDetails objectAtIndex:0];
movie.movie_year = [[movieDetails objectAtIndex:1] intValue];
movie.movie_director = [movieDetails objectAtIndex:2];
movie.movie_cast = [movieDetails objectAtIndex:3];
movie.movie_genre = [movieDetails objectAtIndex:4];
[self.table reloadData];
[self.table setEditing:editing animated: YES];
[super setEditing:editing animated: YES];
[self updateMovie];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"An error occurred!" message:msg delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
}
} else {
[self.table setEditing:editing animated: YES];
[super setEditing:editing animated: YES];
}
}
Now, the strange thing happens when I enter edit mode for the second time consecutively. The first time, the editing style displays correctly, as seen here:
However, once I return from the editing mode and enter it again, the cells look like this:
Has anyone encountered this before?
EDIT: cellForRowAtIndexPath
implementation:
- (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];
}
NSString *cellLabel = [cellDetails objectAtIndex:indexPath.row];
NSString *cellData = [movieDictionary objectForKey:cellLabel];
cell.textLabel.text = [[NSString alloc] initWithFormat:@"%@ %@", cellLabel, cellData];
cell.editingAccessoryType = UITableViewCellAccessoryNone;
cell.accessoryType = UITableViewCellAccessoryNone;
return cell;
}