これはUITableViewControllerを拡張した単純なクラスであり、セクションは1つだけで、合計5行です。ただし、アプリを起動すると、2つのレコードのみが表示されます。ボタンを押すと、テキストフィールドでさらに多くのレコードが表示されます。
しかし、シンプルボタンを押して詳細ボタンを押すと、表の順序が間違っていることがわかりました。
何が起こるかわからないので、この問題を解決するのに役立ちますか?どうもありがとう
- (void)viewDidLoad
{
[super viewDidLoad];
tempSimpleAry = [@[@"simple1",@"simple2"] mutableCopy];
tempDetailAry = [@[@"detail A",@"detail B",@"detail C"] mutableCopy];
dataAry = [[NSMutableArray alloc]init];
[dataAry addObjectsFromArray:tempSimpleAry];
isExpanded = NO;
}
-(void)dealloc{
[super dealloc];
tempSimpleAry = nil;
tempDetailAry = nil;
dataAry = nil;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (BOOL)textFieldShouldReturn:(UITextField *)aTextField {
[aTextField resignFirstResponder];
return YES;
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return dataAry.count+1;
}
static NSString *BasicCellIdentifier = @"BasicCell";
static NSString *MyCellIdentifier = @"MyCell";
static NSString *LastCellIdentifier = @"LastCell";
UITableViewCell *Lastcell = nil;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
int row = indexPath.row;
if(indexPath.row<tempSimpleAry.count){
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:BasicCellIdentifier forIndexPath:indexPath];
cell.textLabel.text = [[NSString alloc]initWithFormat:@"%@ %d", [dataAry objectAtIndex:row], row];
return cell;
}else if(indexPath.row < dataAry.count){
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyCellIdentifier forIndexPath:indexPath];
cell.myTitleLabel.text = [[NSString alloc]initWithFormat:@"%@ %d", [dataAry objectAtIndex:row], row];
cell.myTextField.placeholder = [[NSString alloc]initWithFormat:@"%@ %d", [dataAry objectAtIndex:row], row];
NSLog(@"MyTableViewCell - myTitlelabel = %@, mytextfield tag = %d, text = %@", cell.myTitleLabel.text, cell.myTextField.tag,cell.myTextField.text);
cell.myTextField.tag =100+indexPath.row;
return cell;
}else{
Lastcell = [tableView dequeueReusableCellWithIdentifier:@"LastCell" forIndexPath:indexPath];
Lastcell.textLabel.text = @"Detail";
return Lastcell;
}
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[currentEditingTextField resignFirstResponder];
NSMutableArray *tmpAry = [[NSMutableArray alloc]init];
if(indexPath.row == dataAry.count){
if(isExpanded){
int idx = dataAry.count-1;
for(int i=0; i<tempDetailAry.count;i++){
NSIndexPath *idxPaths = [NSIndexPath indexPathForRow:idx-i inSection:0] ;
[tmpAry addObject:idxPaths];
[dataAry removeLastObject];
}
[tableView deleteRowsAtIndexPaths:tmpAry withRowAnimation:UITableViewRowAnimationFade];
}else{
int idx = tempSimpleAry.count;
for(int i=0; i<tempDetailAry.count;i++){
NSIndexPath *idxPaths = [NSIndexPath indexPathForRow:idx+i inSection:0] ;
[tmpAry addObject:idxPaths];
[dataAry insertObject:[tempDetailAry objectAtIndex:i] atIndex:idx+i];
}
[tableView insertRowsAtIndexPaths:tmpAry withRowAnimation:UITableViewRowAnimationFade];
}
[self.tableView beginUpdates];
[self.tableView endUpdates];
isExpanded = !isExpanded;
if(!isExpanded)
Lastcell.textLabel.text = @"Detail";
else
Lastcell.textLabel.text = @"Simple";
}
}