0

これは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";
    }


}
4

2 に答える 2

1

これは、セルが cellIdentifier でキャッシュされているためです。

Simpleを押すと、「詳細 C 4」、「詳細 B 3」、「詳細 A 2 」の順にセルを削除します。したがって、「詳細 C 4」セルが最初にキャッシュ キューにプッシュされ、次に「詳細 B 3」セル、最後に「詳細 C 2」セルがプッシュされます。

次にDetailを押すと、tableViewがキャッシュ キューを 1 つずつクエリします。「detail C 4」セルは先頭にあるため、最初にポップされます。したがって、textField で「aa」を含むセルを使用し、次に cell.myTitleLabel.text と cell.myTextField.placeholder を設定しますが、「aa」である cell.myTextField.text を設定しません。最終的に、セルは「詳細 A 2」になり、textField に「aa」というテキストが表示されました。

他の 2 つのセルも同様です。

上記はあなたにとって明らかですか?

于 2013-01-07T06:50:00.770 に答える
0

テキストではなくテキストフィールドのプレースホルダーを変更しているだけのようです。テキストフィールドのテキストを配列に保存して、cellForRowAtIndexPathに設定する必要があります。

于 2013-01-07T05:18:41.707 に答える