【问题标题】:Two UITableViews in one file一个文件中有两个 UITableView
【发布时间】:2023-03-27 23:10:02
【问题描述】:

是否可以有两个 UITableViews 使用同一个控制器文件?如果是这样,您如何区分两者?如果没有,你将如何控制它们?

【问题讨论】:

    标签: iphone objective-c ipad uitableview


    【解决方案1】:

    肯定是的。为每个表视图创建两个实例变量。

    表视图委托方法包括一个指向表视图的指针,在方法中调用它们,

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        if(tableView != [self tableView1]){
          //do stuff
        }
        //handle tableView2
        else{
    
        }
    

    【讨论】:

      【解决方案2】:

      是的,这是可能的。

      由于 UITableViewDelegate 和 UITableViewDataSource 接口的方法签名,差异是可能的。如果您查看任一界面,您会注意到它们都传递了对调用该方法的 UITableView 实例的引用...

      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
      

      查看UITableViewDelegateUITableViewDataSource API 文档了解更多信息。

      【讨论】:

        【解决方案3】:

        是的,正如其他海报所示,这是可能的。
        但是还有另一种方法,每个 TableView 都有自己的控制器。这些控制器在另一个 UIViewController 中被实例化。这样耦合度较低,因此重用能力更强。两个 UIViewController 之间的通信使用委托。
        Have a look on my sample code.

        【讨论】:

          【解决方案4】:

          为你的 UITableViews 使用不同的标签。

          【讨论】:

            猜你喜欢
            • 2022-06-13
            • 1970-01-01
            • 1970-01-01
            • 2016-04-14
            • 1970-01-01
            • 2020-12-06
            • 1970-01-01
            • 2011-01-14
            • 2013-08-20
            相关资源
            最近更新 更多