0

这是一个扩展 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";
    }


}
4

2 回答 2

1

这是因为单元格是使用 cellIdentifier 缓存的。

当您按Simple时,您按顺序删除单元格,“ detail C 4 ”、“ detail B 3 ”、“ detail A 2 ”。所以“detail C 4”单元首先被推入缓存队列,然后是“detail B 3”单元,最后是“detail C 2”单元。

然后按Detail,tableView会一一查询缓存队列。因为“detail C 4”单元格在前面,所以首先弹出。所以你在textField中使用带有'aa'的单元格,然后设置cell.myTitleLabel.text和cell.myTextField.placeholder,但你没有设置cell.myTextField.text,即'aa'。最后,单元格变成了“detail A 2”,在 textField 中带有文本“aa”。

与其他两个单元格相同。

以上对你来说清楚吗?

于 2013-01-07T06:50:00.770 回答
0

您似乎只是更改了文本字段的占位符而不是其文本,您还应该将文本字段的文本保存到一个数组中并将其设置在 cellForRowAtIndexPath 中!

于 2013-01-07T05:18:41.707 回答