2

UITableView を作成して、多くの設定 UITableViews と同様に、ユーザーがデータを入力できるようにしようとしています。アプリのこのような一般的な機能の場合、これはあまり簡単ではないようです。

カスタム UITableViewCell から indexPath.row にアクセスできません。ここで、カスタム UITableViewCell を割り当てます。

UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[TextFieldCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];      
}

私の TextField クラスの @implementation では、 - (id)initWithStyle: で indexPath にアクセスしようとしています:

NSIndexPath *indexPath = [(UITableView *)self.superview indexPathForCell:self];

... テキストフィールドのタグを次のように設定するには:

textRow = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 200, 21)];
textRow.tag = [indexPath row];

誰でも私の問題に光を当てるか、基本的な設定スタイルのTableViewをプログラムで作成する方向に向けてください。

ありがとうございました

4

1 に答える 1

3

問題は、セルの初期化時にセルが UITableView に追加されていないことにあると思います。したがって、self.superview は nil を返します。

UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[TextFieldCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];      
}
[cell setTag:indexPath.row];

setTag: メソッドを TextFieldCellClass に追加する必要があります。コードは cellForIndexPath などの内部にあると思われるため、 indexPath がそのメソッドに渡されます。

set tag メソッドは次のようになります。

-(void)setTag:(int)tag {
    textRow.tag = tag;
}
于 2011-02-19T13:32:28.370 に答える