在UICollectionView reloadData()之后UITextfield成为FirstResponder

时间:2016-04-20 15:51:47

标签: swift uicollectionview uitextfield

在我的UICollectionView中,我使用第一个单元作为搜索栏,因此每次用户键入一些字母时,我都会通过调用reloadData()来获取数据然后刷新视图。

我的问题是,在重新加载数据后,我无法将UITextField设为becomeFirstResponder()

在表格视图中,我可以将带有搜索字段的单元格放入单独的部分,因此我只需要重新加载另一部分来刷新该内容。

但我怎样才能在UICollectionView中使其发挥作用?

cellForItemAtIndexPath

中我的代码示例
if indexPath.row == 0 && self.showSearch {

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("searchCell", forIndexPath: indexPath)

    if self.searchField == nil {
        if let searchField = cell.viewWithTag(1) as? UITextField {
            self.searchField = searchField
            self.searchField!.addTarget(self, action: #selector(MyCollectionViewController.searchFieldChanged(_:)), forControlEvents: UIControlEvents.EditingChanged)
        }
    }


    return cell
}

1 个答案:

答案 0 :(得分:0)

重新加载除带有搜索字段的单元格以外的所有单元格,这应该可以工作。

相关问题