在 tvOS 的 TableViewCell 中聚焦 UITextField

Focus UITextField in TableViewCell on tvOS

在我的 AppleTV 应用程序中,我有一个带有 UITextField 的自定义 UITableViewCell。

我想要实现的是能够聚焦此文本字段以在其中输入文本。

不幸的是,只有单元格本身在聚焦,我无法聚焦文本字段。

谢谢

单元格正在聚焦,因为它 returns YES 来自 canBecomeFocused。如果您希望单元格内的某些内容成为焦点,则单元格需要从该方法开始 returning NO。有两种方法可以做到这一点:使用 table 视图委托方法,或者通过子类化 UITableViewCell.

在您的 table 视图的委托中,您可以为包含文本字段的单元格实施方法 tableView:canFocusRowAtIndexPath: 和 return NO

或者,在 UITableViewCell 的子类中,您可以将 canBecomeFocused 重写为 return NO.

子类化 UITableViewCell 并将单元格的 preferredFocusedView 设置为 UITextField。

@interface TableViewCell : UITableViewCell
@property (nonatomic, weak) IBOutlet UITextField *textField;
@end

@implementation TableViewCell
...
- (UIView*)preferredFocusedView {
  return self.textField;
}