-2

次の機能を備えた UITableView を作成するにはどうすればよいですか。

UITableView セルに UITextfields を入れたいと思います。ユーザーが UITextField への入力を開始すると、テーブルビューはそのすぐ下に新しい同一のセルを自動的に生成します。これは基本的に、「無限」の数のフィールドを持つフォーム用です。

これを書く上でのポインタ/ヒントは素晴らしいでしょう!

ストーリーボードの質問 1 でカスタム セルを作成する

ストーリーボードでカスタム セルを作成するとき、ビュー コントローラーのテーブルビュー内に「カスタム セル」を作成するだけでよいですか? もしそうなら、セルが継承するクラスを変更する必要がありますか?

ここに画像の説明を入力

ストーリーボードの質問 2 でカスタム セルを作成する

Cell Identifier の用途を簡単に説明していただけますか?

ここに画像の説明を入力

追加コード:

カスタム UITableViewCell を作成するには:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"AddListInformationCellid";
    AddListInformationCell *cell = (AddListInformationCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"AddListInformationCell" owner:self options:nil];
        for (id currentObject in topLevelObjects) {
            if ([currentObject isKindOfClass:[UITableViewCell class]]) {
                cell = (AddListInformationCell *)currentObject;
                break;
            }
        }
    }

    return cell;
}

どうもありがとう。

4

2 に答える 2

1

テキスト フィールドを含むカスタム セルでこれを行います。以下の例では、テキスト フィールドとラベルを持つ (ストーリーボードで作成された) セルがあります。ユーザーが入力を開始すると、エントリ (文字列 "New" のみ) を配列に追加し、ユーザーが入力しているセルの下に新しいセルを挿入します。

- (void)viewDidLoad {
    [super viewDidLoad];
    self.theData = [@[@"One",@"Two",@"Three",@"Four"] mutableCopy];
    [self.tableView reloadData];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.theData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    RDCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    cell.label.text = self.theData[indexPath.row];
    return cell;
}

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

-(void)textFieldDidBeginEditing:(UITextField *)textField {
    self.beganEditing = YES;
}

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if (self.beganEditing) {
        NSInteger row = [self.tableView indexPathForCell:(UITableViewCell *)textField.superview.superview].row;
        [self.theData insertObject:@"New" atIndex:row + 1];
        [self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:row + 1 inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic];
        self.beganEditing = NO;
    }
    return YES;
}
于 2013-07-15T01:13:03.457 に答える
-2

contentViewセルのプロパティを使用する必要があります。UITableViewDelegateView Controller が、、、UITableViewDataSourceおよびUITextViewDelegateプロトコルに準拠していることを確認してください。メソッドには、cellForRowAtIndexPath次のようなものが必要です。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(30, 7, 150, 30)];
    [textField setDelegate:self];
    [textField setBorderStyle:UITextBorderStyleRoundedRect];
    [cell.contentView addSubview: textField];
    [cell.textLabel setText:@""];
    return cell;
}

次に、textFieldShouldBeginEditingメソッドで、必要なセルの数に 1 を追加しnumberOfRowsInSection:、データソースのメソッドでその数を使用します。

于 2013-07-14T23:08:03.240 に答える