構築中のアプリケーションに UITableView を使用しています。UITableView が編集モードになると、ここで定義されているように、テーブル セルがセルの上にテキスト フィールドを表示します。
- (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;
}
次に、setEditing
実装で:
-(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];
}
}
さて、2回連続で編集モードに入ると奇妙なことが起こります。次に示すように、最初は編集スタイルが正しく表示されます。
ただし、編集モードから戻って再度入力すると、セルは次のようになります。
誰もこれに遭遇したことがありますか?
編集: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];
}
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;
}