【问题标题】:Adding UITextfield to UITableviewCell causes detailTextLabel to slide "up"将 UITextfield 添加到 UITableviewCell 会导致 detailTextLabel “向上”滑动
【发布时间】:2013-08-20 02:02:01
【问题描述】:

我正在尝试将 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];

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

【问题讨论】:

    标签: ios uitableview


    【解决方案1】:

    如果没有看到您的代码,就很难知道发生了什么。这是我用来将 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; }
    

    【讨论】:

    • 似乎在创建时将文本字段构建到单元格中比以后更容易。它现在工作正常。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-24
    • 1970-01-01
    • 2011-10-21
    • 2016-04-28
    • 2014-07-28
    相关资源
    最近更新 更多