1

我正在尝试将 UITextField 添加为 UITableViewcell 的 contentView。它有效,并且该字段具有焦点,但 detailTextLabel 向上滑动 10px。这是怎么回事?作为第二个问题,是否有更好的方法来创建内联可编辑 UITableViewCell?

这是我的代码。我正在尝试通过双击表格视图单元格来执行此操作。我检索像这样点击的实际单元格:

CGPoint swipeLocation = [recognizer locationInView:self.tableView];
NSIndexPath *swipedIndexPath = [self.tableView indexPathForRowAtPoint:swipeLocation];
UITableViewCell *swipedCell;

然后我尝试像这样添加我的文本字段:

swipedCell = [self.tableView cellForRowAtIndexPath:swipedIndexPath];
NSInteger indent = swipedCell.indentationWidth;
UITextField * newText = [[UITextField alloc] initWithFrame:CGRectMake(indent, swipedCell.contentView.frame.origin.y, swipedCell.contentView.frame.size.width, swipedCell.contentView.frame.size.height)];
newText.font = swipedCell.textLabel.font;
newText.text = swipedCell.textLabel.text;
newText.textColor = [UIColor grayColor];
newText.autoresizingMask =  UIViewAutoresizingFlexibleWidth |  UIViewAutoresizingFlexibleHeight;
swipedCell.textLabel.text = @"";
[swipedCell addSubview:newText];
[newText becomeFirstResponder];

与您的完全不同,除了当我应用您的代码时,我的文本和详细标签都位于单元格的中心。

4

1 回答 1

2

如果没有看到您的代码,就很难知道发生了什么。这是我用来将 UITextField 作为 contentView 单元格的代码,它可以正常工作,希望对您有所帮助。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"TaxCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
CGFloat optionalRightMargin = 10.0;
CGFloat optionalBottomMargin = 10.0;
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(275, 10, cell.contentView.frame.size.width - 270 - optionalRightMargin, cell.contentView.frame.size.height - 10 - optionalBottomMargin)];


textField.placeholder = @"Placeholder";
textField.delegate = self;
textField.autoresizingMask =  UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;

textField.text = @"Textfield text"
cell.textLabel.text = @"Textlabel text"
cell.detailTextLabel.text = @"Detail text";
[cell.contentView addSubview:textField];

return cell; }
于 2013-08-20T02:07:22.460 回答