ios-给tableview Cell 添加不同的控件
- (void)viewDidLoad {
[super viewDidLoad];
_tableView = [[UITableView alloc] initWithFrame: CGRectMake(10, 90, 300, 200) style: UITableViewStyleGrouped];
_tableView.delegate = self;
_tableView.dataSource = self;
[self.view addSubview: _tableView];
}
#pragma mark uitableview delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *baseCell = @"base";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: baseCell];
if(!cell)
{
cell = [[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleValue1 reuseIdentifier: baseCell] autorelease];
}
if(2 == indexPath.row)
{
cell.textLabel.text = @"年龄: ";
UITextField *tf = [[UITextField alloc] initWithFrame: CGRectMake(65.0, 5.0, 160.0, 35.0f)];
tf.borderStyle = UITextBorderStyleRoundedRect;
tf.delegate = self;
tf.placeholder = @"输入年龄";
tf.returnKeyType = UIReturnKeyDone;
[cell.contentView addSubview: tf];
return cell;
}
if(0 == indexPath.row)
{
cell.textLabel.text = @"1";
return cell;
}
if(1 == indexPath.row)
{
cell.textLabel.text = @"2";
return cell;
}
return cell;
}
#pragma mark UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}