将UITextfield添加到UITableviewCell会导致detailTextLabel向上滑动

时间:2013-08-20 02:02:01

标签: ios uitableview

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

这是我的代码。我试图通过双击tableview单元格来做到这一点。我检索了这样点击的实际单元格:

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];

与你的完全不同,除了当我应用你的代码时,我的文本和细节最终都在单元格的中心。

1 个答案:

答案 0 :(得分: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; }