【问题标题】:How to replace a UITableView by UICollectionView in a same UIViewController?如何在同一个 UIViewController 中用 UICollectionView 替换 UITableView?
【发布时间】:2023-03-24 14:13:01
【问题描述】:

我有一个 UIViewController,这个控制器有一个搜索栏。如果我写了一些东西,搜索结果将显示在 UITableView 上。当我选择 UITableView 的某一行时,UITableView 将消失,UICollectionView 将出现在同一个 UIViewController 上。

示例:如果我在 Searchbar 中写 f,UITableView 会显示

feni
florida
france

如果我在法国按,详细信息将显示在同一 UIViewController 中的 UICollectionView 上。

有人认为我曾想过在 DidSelectRowAtindexPath 中隐藏 UITableView 并以编程方式出现 UICollectionview ...但是以编程方式设计 UICollectionview 的单元格对我来说并不容易。

请提供我可以继续的任何建议或教程。

【问题讨论】:

  • 您可以使用故事板在同一个视图控制器中设计collectionview和tableview。您可以根据需要隐藏/取消隐藏tableview/collectionview。
  • 将 uicollectionview 设置在 uitableview 之上或反之亦然是个好主意???如果我需要对 UICollectionView 下的 UITableViewCell 进行任何更改,那么设计对我来说似乎是一场噩梦。 :(
  • 但是你可以隐藏你的collectionview或tableview一段时间来改变tableview或collectionview单元格的设计,反之亦然
  • 在设计 UICollectionView 下的 UITableViewcell 时.....我将在 StoryBoard 中保留/隐藏 UICollectionView ???
  • 如果要设计collectionview下方的tableview单元格,则需要隐藏collectionview或更改collectionview的框架,直到完成设计,然后重新排列它之前的样子

标签: ios uitableview uiviewcontroller swift3 uicollectionview


【解决方案1】:

你可以创建一个带有tableview和collection view的storyboard,并给出storyboard标识符并根据条件加载storyboard

    if (some_condition)
    {
       //UITableview storyboard
      UIStoryboard* storyBoard  = [UIStoryboard storyboardWithName:@"storyboardName" bundle: nil]; 
      ViewController* vc = [storyBoard instantiateViewControllerWithIdentifier: @"Storyboard identifier"];
    }
    else
    {
      //UICollectionview storyboard
       UIStoryboard* storyBoard  =  [UIStoryboard storyboardWithName:@"storyboardName For Collection"  bundle: nil]; 
       ViewController* vc =  [storyBoard instantiateViewControllerWithIdentifier: @"Storyboard identifier for collection view"];
    }

希望对你有帮助!

【讨论】:

  • 您可以在 uitableview 的 DidSelectRowAtindexPath 中加载集合视图的第二个故事板,它将替换您的 tableview。由于两个 viewController 是相同的。在 ViewController 中你需要同时实现 tableview 和 collection view
猜你喜欢
  • 2021-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-26
  • 1970-01-01
  • 2014-03-02
相关资源
最近更新 更多