我正在尝试以编程方式在我的一个tableview单元格中添加TextField。我该怎么做?
假设它是在以下方法中,我将使用什么代码?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier] autorelease];
cell.editingAccessoryType = UITableViewCellAccessoryDisclosureIndicator;
// Don't let the user click when not in editing mode
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
switch (indexPath.row) {
case 0:
cell.textLabel.text = @"Title";
// This is where I want to add a text field
cell.detailTextLabel.text = @"test";
cell.editing = YES;
break;
}
return cell;
}
答案 0 :(得分:1)
试试这个:
[cell.contentView addSubview:[[[UITextField alloc] initWithFrame:CGRectMake(...)] autorelease]];