5

これはおそらく設計が悪いことが原因ですが、テーブルをスクロールする速度が速すぎて一番上に戻ると、最後のテーブル セルに配置されているビューが tableView の最初のテーブル セルの上にも配置されます。

おそらく、if ステートメントを使用して、最初のセクションに静的コンテンツを配置し、2 番目のセクションに動的コンテンツを配置したことが原因だと思います。

どんな助けでも大歓迎です。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // Configure the cell...

    if (indexPath.section == 0) {

        if (indexPath.row == 0) {

            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(10, 13, 282, 20)];
            textField.clearsOnBeginEditing = NO;
            textField.placeholder = @"enter template name";
            textField.font = [UIFont systemFontOfSize:15.0];
            textField.clearButtonMode = UITextFieldViewModeWhileEditing;
            textField.delegate = self;
            textField.text = [selectedTemplate name];
            [textField addTarget:self action:@selector(nameDidChange:) forControlEvents:UIControlEventEditingChanged];
            [cell.contentView addSubview:textField];

        } else {

            cell.textLabel.text =  @"Add a new question";
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

        }

    } else {

        NSString *label = [[sortedQuestions valueForKey:@"questionText"] objectAtIndex:indexPath.row];
        CGSize stringSize = [label sizeWithFont:[UIFont boldSystemFontOfSize:15] constrainedToSize:CGSizeMake(230, 9999) lineBreakMode:NSLineBreakByWordWrapping];

        UITextView *textV=[[UITextView alloc] initWithFrame:CGRectMake(5, 5, 230, stringSize.height+10)];
        textV.font = [UIFont systemFontOfSize:15.0];
        textV.text=label;
        textV.textColor=[UIColor blackColor];
        textV.editable = NO;
        textV.userInteractionEnabled = NO;
        textV.backgroundColor = [UIColor colorWithRed:0.97f green:0.97f blue:0.97f alpha:1.00f];
        [cell.contentView addSubview:textV];

        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    }

    return cell;
}
4

3 に答える 3

5

これが、Wienkeの推奨に基づいて私が最終的に行ったことです。Cell、Cell2、Cell3 という名前のストーリーボードに 3 つのプロトタイプ セルを追加しました。

関連コード:

static NSString *CellIdentifier = @"Cell";
static NSString *CellIdentifier2 = @"Cell2";
static NSString *CellIdentifier3 = @"Cell3";

UITableViewCell *cell;

if (indexPath.section == 0 && indexPath.row == 0) {

    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

} else if (indexPath.section == 0 && indexPath.row == 1) {

    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2 forIndexPath:indexPath];

} else if (indexPath.section == 1) {

    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier3 forIndexPath:indexPath];

}

また、これを追加して、動的セルをチェックし、新しいサブビューを追加する前に、残っているサブビューを削除しました。

    if ([cell.contentView subviews]){
        for (UIView *subview in [cell.contentView subviews]) {
            [subview removeFromSuperview];
        }
    }
于 2012-11-02T03:12:28.633 に答える
1

たとえば、セクション 0 行 0 用に配置されたセルがデキューされ、セクション 0 行 1 などに使用されているように見えますが、セクション 0 を保持していたときに作成されたすべての設定を置き換えるわけではありません。行 0 のマテリアル。

セクション 0 行 0 に 1 つ、セクション 0 行 1 に 1 つ、残りすべてに 1 つの、3 つの個別のセル識別子の使用を検討することをお勧めします。を呼び出すときに正しい識別子を使用できるように、if ステートメント (または switch-case ステートメント) の予備セットが必要ですdequeueReusableCellWithIdentifier:

于 2012-11-02T02:02:47.067 に答える