【问题标题】:UITextField inside UICollectionViewCell not reachableUICollectionViewCell 内的 UITextField 无法访问
【发布时间】:2014-10-30 13:05:04
【问题描述】:

感谢您的阅读,

我在UICollectionViewCell 中有两个UITextFields(以及一些标签)。我可以在模拟器中选择两个文本字段,适用于 iPhone 6、6+、5、5s、4s,所有 iOS8

当我连接我的 iPhone,一个 iPhone 5c iOS7(非 sim)时,我只能选择顶部 UITextField 的左上部分。答案将建议如何选择两个文本字段,并探索其他可能的原因有人无法选择文本字段,以及可能的调试方法。对于那些想知道的人,一旦我让这个应用程序在 ios7 中 100% 运行,我就会升级到 ios8。

我尝试过的解决方案:

  1. Make sure the textField is inside the known superview
  2. 创建一个新项目,使其正常工作,并尽可能准确地复制所有内容
  3. 移动文本字段,使底部的在顶部
  4. 这让我在同一个小点击区域中选择了新的“顶部”字段。
  5. 选择 UICollectionView 单元并自动设置第一响应者。这不起作用,因为我希望用户选择哪个字段作为第一响应者。
  6. 将所有背景元素设置为不同的颜色,以确保没有任何内容与 TextField 重叠
  7. Set UICollectionView to be Editable -- 它只适用于UITableViews
  8. 确保 collectionview 元素的顺序正确
  9. 复制并粘贴“Good Text Field”,然后查看是否可以选择。
  10. Deleting the entire ViewController and starting again.
  11. 确保UICollectionView 单元格具有“启用用户交互

我的当前设置

  • Heres a screenshot of the storyboard
  • UICollectionView: data-source & delegate = it's superview (UIViewController)
  • UICollectionViewCell: 有 4 个出口链接到一个自定义单元
  • UITextFields:代表设置为UIViewController
    • 通向单元的参考插座
    • EditingDidBeginEditingDidEnd 方法在 UIViewController 中处理
    • 一个文本字段使用选择器输入视图,另一个使用键盘

要明确:

  1. 我可以让键盘在模拟器中弹出,在我的手机上的小点击区域中弹出
  2. This is not about retrieving the data from text fields.
  3. 我可以让测试项目正常工作,但主项目拒绝接受UITextFields 中的点击

相关代码 - UIViewController .m

接口 - Private.m

@interface EnterFinalHRViewController () 
// The current responder showing a keyboard.
@property (nonatomic, assign) id currentResponder;

文本字段方法

- (IBAction)editingDidBegin:(UITextField *)textField {
self.currentResponder = textField;
}
- (IBAction)resignOnTap:(id)sender {
//called from a single tap on the view, gesture recognizer is present
//called when the text field says "Editing did End"
[self.currentResponder resignFirstResponder];
}

 //implementation of the cell

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
             cellForItemAtIndexPath:(NSIndexPath *)indexPath{

static NSString *cellIdentifier = @"swimmerReview";
FinalHeartRateCollectionViewCell *cell = (FinalHeartRateCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath]; //reuse the cell

Lane *lane = [_lanes objectAtIndex:indexPath.section]; //lanes hold swimmers, so get the lane
Swimmer *swimmer = [lane.swimmers objectAtIndex:indexPath.row]; //swimmers are in the lane, so get the right swimmer

if (swimmer.actualSwimmerName) {
    cell.swimmerFullName.text = swimmer.actualSwimmerName;
} else {
    cell.swimmerFullName.text = @"Assign Swimmer";
}

cell.finalHeartRate.text = @"--";

//Person Picker is a PickerView that lets the user select a swimmer
cell.swimmerFullName.inputView = PersonPicker;

cell.averageStrokeRate.text = [NSString stringWithFormat:@"Avg SR: %.f", swimmer.strokeRateAvg];
cell.AveragePace.text = [@"Avg Pace: " stringByAppendingString: swimmer.setSplitAvg];

return cell;
}

问题

答案将建议如何选择两个文本字段,并探索其他可能的原因有人无法选择文本字段,以及可能的调试方法。

快速向 SO 的所有贡献者大喊,你们都是这样的资源,我从你们回答的所有问题中阅读并学到了很多东西。这次我实在想不通。

【问题讨论】:

    标签: ios objective-c iphone uitextfield uicollectionviewcell


    【解决方案1】:

    @Ian MacDonald,您提供了一个调试方法,该方法导致了这个发现的瑰宝Content View Not Resizing

    我的问题是运行 ios7 与 ios8 之一。在我的自定义单元格中简单地覆盖布局子视图方法解决了这个问题。向@Daniel Plamann 大声疾呼,您可以在上面的页面上为他的解决方案投票。

    //放在你的 UICollectionViewCell 自定义类中

    - (void)layoutSubviews
    {
      [super layoutSubviews];
    
      BOOL contentViewIsAutoresized = CGSizeEqualToSize(self.frame.size, self.contentView.frame.size);
    
      if( !contentViewIsAutoresized) {
        CGRect contentViewFrame = self.contentView.frame;
        contentViewFrame.size = self.frame.size;
        self.contentView.frame = contentViewFrame;
      }
    }
    

    我的测试项目之所以成功,是因为我是在两天前完成的,而我的主要项目是在 ios8 发布之前的 6 个月前完成的。所以如果你发现你做的一切都正确,按照上面的 10 个步骤,你仍然无法选择你的子视图,这里还有一个。

    【讨论】:

      【解决方案2】:

      听起来您的 cell 框架(或其超级视图之一)不够大。一种快速而肮脏的调试方法是添加它并点击您知道可以访问的 textField 的顶角:

      - (IBAction)editingDidBegin:(UITextField *)textField {
        self.currentResponder = textField;
        UIView *superview = textField;
        while (superview != nil) {
          [superview setClipsToBounds:YES];
          superview = [superview superview];
        }
      }
      

      它应该突出显示您遇到框架问题的地方。从那里,您可以准确检测哪个视图的边界错误,并调查您为其设置的自动布局/常规布局属性。

      【讨论】:

      • 你是对的。有一个子视图添加到自定义 UICollectionViewCell(不是我),我所有的界面元素都在里面。在我的测试项目中,未知子视图重新调整为自定义单元格的宽度,但是在我的主项目中,它保持在 50 x 50,剪裁并记录名称显示给我。现在来了解为什么要添加它,以及如何让它扩展以填充父视图。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多