3

ageTextField財産がありますTableViewController。最初のセクションセルのサブビューに追加しましたが、下にスクロールすると別のセクション (#3) に表示される理由がわかりません..

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    }
    // Configure the cell...
    switch (indexPath.section){
        case 0:
        {
            [cell addSubview:self.ageTextField];
        }
        break;
        case 1:
        {
            cell.textLabel.text = [screeningArray objectAtIndex:indexPath.row];
            if (indexPath.row == 0)     //no choice
                cell.accessoryType = self.doneScreening ? UITableViewCellAccessoryNone:UITableViewCellAccessoryCheckmark;
            else                        //yes choice
                cell.accessoryType = self.doneScreening ? UITableViewCellAccessoryCheckmark:UITableViewCellAccessoryNone;
        }
        break;
        case 2:
        {
            cell.textLabel.text = @"1 : ";
            [cell addSubview:self.riskTextField];
        }
        break;
        case 3:
        {
            cell.textLabel.text = [findingsArray objectAtIndex:indexPath.row];
            cell.accessoryType = [[boolValuesArray objectAtIndex:indexPath.row] boolValue] ? UITableViewCellAccessoryCheckmark:UITableViewCellAccessoryNone;
        }
        break;
        default:
        break;
    }
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    return cell;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSUInteger newLength = [textField.text length] + [string length] - range.length;
    if (textField == self.ageTextField)
        return (newLength > 2) ? NO : YES;
    else
        return (newLength > 7) ? NO : YES;
}

これが冒頭のスクリーンショットです..

http://dl.dropbox.com/u/30838210/Screen%20Shot%202012-08-07%20at%209.26.47%20PM.png

下にスクロールした後、ageTextField のプレースホルダーがセクション 3、セル 3 で複製されていることに注目してください。

http://dl.dropbox.com/u/30838210/Screen%20Shot%202012-08-07%20at%209.27.28%20PM.png

もう一度上にスクロールすると、section3 の cell3 のテキストが最初のセルに表示されます。

dl.dropbox.com/u/30838210/Screen%20Shot%202012-08-07%20at%209.27.52%20PM.png

それは奇妙で、何が起こったのか理解できませんでした! 助けてください..

4

2 に答える 2

8

テーブル ビュー セルの内容を再利用する場合 (つまり、セル != nil の場合) は削除する必要があります。また、サブビューは、セル自体ではなくテーブル セルの contentView に追加する必要があるため、編集モードに入る/終了するときにサブビューが適切に調整されます。

        [cell.contentView addSubview:self.ageTextField];

        [cell.contentView addSubview:self.riskTextField];

次のコードは、再利用のためにセルを準備します。

if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
else
{
    // Prepare cell for reuse

    // Remove subviews from cell's contentView
    for (UIView *view in cell.contentView.subviews)
    {
        // Remove only the appropriate views
        if ([view isKindOfClass:[UITextField class]])
        {
            [view removeFromSuperview];
        }
    }
    cell.accessoryType = UITableViewCellAccessoryNone;
    cell.textLabel.text = nil;
}
于 2012-08-08T08:31:40.147 に答える