简体   繁体   English

UITableView里面的UITableView?

[英]UITableView inside a UITableView?

Generally: 通常:

Is it OK to add a UITableView as a subview of another UITableView ? 是否可以将UITableView添加为另一个UITableView的子视图? Or, should I create a UIView and add each UITableView to it? 或者,我应该创建一个UIView并添加每个UITableView吗?

Specifically: 特别:

For a ComposeTableViewController with a typeahead, like in the iPhone's native Mail app, which approach would you recommend and why? 对于具有ComposeTableViewController ,就像iPhone的本机Mail应用程序一样,您会推荐哪种方法?为什么?

Note: I prefer to construct things 100% programmatically (no Interface Builder). 注意:我更喜欢以编程方式100%构建事物(没有Interface Builder)。

  1. Subclass UITableViewController . 子类UITableViewController

    Then, to show the typeahead results, create and add a resultsTableView as a subview of self.tableView positioned directly underneath the the cell (of self.tableView ) with the typeahead text field. 然后,要显示预先输出结果,请创建并添加resultsTableView作为self.tableView的子视图,该子视图直接位于具有typeahead文本字段的单元格( self.tableView )下方。

    The nice thing about this approach is that resultsTableView scrolls with self.tableView automatically. 这种方法的resultsTableViewresultsTableView自动滚动self.tableView

    But, is it OK to add a UITableView as a subview of another UITableView ? 但是,将UITableView添加为另一个UITableView的子视图是否可以?

  2. Subclass UIViewController . 子类UIViewController

    Create and add tableView (custom property) as a subview of self.view . 创建tableView (自定义属性)并将其添加为self.view的子视图。

    Create and add resultsTableView also as a subview of self.view . 创建并添加resultsTableView作为self.view的子视图。

    The annoying thing about this approach is that I have to reposition resultsTableView manually anytime self.tableView scrolls. 关于这种方法的烦人的事情是我必须在self.tableView滚动时手动重新定位resultsTableView

I think I'd prefer approach 1, but adding a UITableView as a subview of another UITableView just seems smelly to me. 我想我更喜欢方法1,但添加UITableView作为另一个UITableView的子视图对我来说似乎很臭。

TableViews cannot have subviews. TableViews不能有子视图。 You can try adding a tableview as the view of a TableViewCell, but then you have to ask yourself how it would scroll, if you tried scrolling in the subtableview would it scroll the child tableview or the parent tableview? 你可以尝试添加一个tableview作为TableViewCell的视图,但是你必须问自己它是如何滚动的,如果你尝试滚动子表视图会滚动子tableview还是父tableview? It is much easier to present multiple tableviews within a view. 在视图中显示多个表视图要容易得多。 This can be done easily by setting your custom viewcontroller as the datasource of both tableviews contained within its view and then comparing the tableview pointer that is sent as a parameter of the datasource method to the two tableview pointers that are IVars of your custom view controller. 这可以通过将自定义viewcontroller设置为其视图中包含的两个tableview的数据源,然后将作为数据源方法的参数发送的tableview指针与作为自定义视图控制器的IVars的两个tableview指针进行比较来轻松完成。

Hold stuff like this in a container. 把这样的东西放在容器里。 Create a container view controller which only does typeahead. 创建一个只进行typeahead的容器视图控制器。 This container view controller will hold your main table view controller (and its table view) and it will hold the typeahead view controller (and its view). 此容器视图控制器将保存主表视图控制器(及其表视图),它将保存预先输入视图控制器(及其视图)。

It will keep the logic needed for the typeahead out of your main table view and as a bonus you might end up with a reusable typeahead container which could be applied to other views. 它将保持主表视图中输出所需的逻辑,作为奖励,您最终可能会使用可重复使用的预先输入容器,该容器可应用于其他视图。

Yes it is good to go for adding UITableView in as a cell of another UITableView . 是的,最好将UITableView添加为另一个UITableView的单元格。

I had one issue and posted a question 我有一个问题并发布了一个问题

Multiple Views as subviews 多个视图作为子视图

And requirement was like a group of controls with list of other controls. 并且需求就像一组控件和其他控件列表。 I thought that time that it will be messy if i'm going to add UITableView as a cell of UITableView . 我想那个时候,这将是混乱的,如果我要添加UITableView作为一个单元UITableView

What i found that... Boy !! 我发现了......男孩! it's how iOS meant to be. 这就是iOS的意思。 There is no any lag while doing this. 这样做没有任何滞后。

Tested for below functionalities: 测试以下功能:

  1. Scrolls like a charm 滚动就像一个魅力
  2. Separate delegates called for each 不同的代表要求各自
  3. Reordering of both UITableView cell 重新排序UITableView单元格
  4. Swipe and remove cell 滑动并移除单元格
  5. Runtime datasource update for both UITableView and reload both at the same time. UITableView运行时数据源更新同时重新加载。

There is no any reason not to add subview UITableView . 没有任何理由不添加子视图UITableView

You can do it but it is against the principle of MVC. 你可以这样做,但这违反了MVC的原则。 You will be setting the delegate of a view to another view which is wrong. 您将视图的委托设置为另一个错误的视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM