这是一个扩展 UITableViewController 的简单类,只有一个部分,总共 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";
}
}