UICollectionView reloadData resigns first responder/dismisses keyboard when searchBar is in section header

前端 未结 2 1720
北荒
北荒 2020-12-09 09:51

I have a UICollectionView that have section header. In the section header I have a UISearchBar. I want to filter the content in my collection view

2条回答
  •  天涯浪人
    2020-12-09 10:38

    I finally figured this out after trying many different options. The solution is to make your header of its own section so you can reload the other sections independently without reloading the one your header is attached to.

    So:

    • Section 0
      • Header
        • Search bar (or other text fields)
      • Rows
        • (None)
    • Section 1
      • Header
        • Create an empty UICollectionReusableView and override ...sizeForItemAtIndexPath:(NSIndexPath *)indexPath to return CGSizeZero
      • Rows
        • Your actual rows that would have originally gone with section 0

    Then, we you need to reload your data:

    [collectionView reloadSections:[NSIndexSet indexSetWithIndex:1]];

    Your searchbar or text fields should retain their focus while the user is typing and you can update the results in the background.

提交回复
热议问题