我在编辑uitextfield后尝试在集合视图中选择第一个单元格。
在函数func textFieldDidEndEditing(textField: UITextField)
中调用函数incandescenteCollection.selectItemAtIndexPath(NSIndexPath(forRow: 0 , inSection: 0), animated: true, scrollPosition: UICollectionViewScrollPosition.CenteredHorizontally)
,其中incandescenteCollection是IBOutlet到集合视图。@IBOutlet weak var incandescenteCollection: UICollectionView!
但不行。有什么想法吗?
答案 0 :(得分:0)
确保您的控制器具有UITextFieldDelegate
协议,并且问题委托的文本字段设置为self
(控制器)。
class ViewController: UIViewController, ..., UITextFieldDelegate {
// MARK: Outlets
@IBOutlet weak var incandescenteCollection: UICollectionView!
@IBOutlet weak var textField: UITextField! {
didSet {
// If the text field is in your cell,
// move this line to cellForItemAtIndexPath
// as cell.textField.delegate = self
textField.delegate = self
}
}
...
// MARK: UITextFieldDelegate
func textFieldDidEndEditing(textField: UITextField) {
incandescenteCollection.selectItemAtIndexPath(
NSIndexPath(forRow: 0 , inSection: 0),
animated: true,
scrollPosition: .CenteredHorizontally
)
}
}