今度はコード上からもう少し簡単にTextFieldを追加する方法を。
カスタムTableViewCellのときと同様にアプリケーションを Master-Detail Application として作成しておきます。
次に、MasterViewController.m の tableView:cellForRowAtIndexPath:を以下のように修正します。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
UITextField *txtField = [[UITextField alloc] init];
[txtField addTarget:self action:nil forControlEvents:UIControlEventAllEditingEvents];
txtField.frame = CGRectMake(10.f, 10.f, cell.contentView.bounds.size.width - 20.f, 24.f);
txtField.returnKeyType = UIReturnKeyDone;
txtField.delegate = self;
[cell addSubview:txtField];
[txtField release];
}
return cell;
}
TextFieldを追加するコードとしてはこれだけです。
カスタムTableViewCellを作るのに比べるとそう複雑ではないと思います。
0 件のコメント:
コメントを投稿