【问题标题】:Use delegate methods when using multiple tableviews in a view controller在视图控制器中使用多个表视图时使用委托方法
【发布时间】:2013-02-20 11:13:39
【问题描述】:

在我的应用程序中,我想在单个 ViewController 中使用 3 个表格视图。问题是如何单独使用UITableViewDelegate 方法。例如;我可以通过标记为每个UITableView 单独使用cellForRowAtIndexPath 方法。但是,我不知道对每个 tableview 以不同的方式使用 numberOfRowsInSectionnumberOfSectionsInTableView 方法。有可能吗?

【问题讨论】:

    标签: iphone ios objective-c uitableview


    【解决方案1】:

    YourViewController.h 中创建 3 个 UITableView 变量:

    YourViewController : UIViewController
    {
        UITableView* tableView1;
        UITableView* tableView2;
        UITableView* tableView3;
    }
    

    YourViewController.m:

    -(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
    {
    
        if (tableView == tableView1)
        {
            //Your code
        }
        if (tableView == tableView2)
        {
            //Your code
        }
        if (tableView == tableView3)
        {
            //Your code
        }
    }
    

    【讨论】:

      【解决方案2】:

      在数据源和委托方法中包含条件的所有表只使用一种数据源和委托方法。

             - (NSInteger)numberOfRowsInSection:(NSInteger)section;
            {
           return 1;
           }
        - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:  (NSInteger)section;
         {
      if (tableView==tabl1) {
              return [arr1 count];
      }
      if (tableView==tabl2) {
              return [arr2 count];
      
      }
      if (tableView==tabl3) {
              return [arr3 count];
      }
      return 0;
      
         }
      
        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
       {
      static NSString *CellIdentifier = @"Cell";
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
      if (cell==nil) {
          cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier ];
          cell.selectionStyle=UITableViewCellSelectionStyleNone;
          cell.backgroundColor=[UIColor clearColor];
      }
      if (tableView==tabl1) {
          cell.textLabel.text = [arr1 objectAtIndex:indexPath.row];
      }
      if (tableView==tabl2) {
          cell.textLabel.text = [arr2 objectAtIndex:indexPath.row];
      }
      if (tableView==tabl3) {
          cell.textLabel.text = [arr3 objectAtIndex:indexPath.row];
      }
      return cell;
            }
      

      【讨论】:

        【解决方案3】:

        您不应该使用单独的委托方法。相反,在像 cellForRowAtIndexPath 这样的每个委托方法中,您应该将您的表标识为

        if(tableview == TableView1) 
        { 
        
        }
        else if(tableview == TableView2)
        {
        
        }
        else
        {
        
        }
        

        等等。这是正确的方法,因为您操作的任何表都将具有通用委托方法,然后您只需要指定表的名称。

        【讨论】:

          【解决方案4】:

          当然:

          所有 tableview 委托函数都将 tableView 作为第一个参数,因此您只需跟踪三个表视图并在每个委托函数中检查委托调用是针对哪个表视图:

          - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
          {
              if(tableView == firstTableView) {
              ...
              }
              else if (tableView == secondTableView) {
              ...
              }
              else if (tableView == thirdTableView) {
              ...
              }
          }
          

          【讨论】:

            【解决方案5】:

            您还可以创建三个表格视图控制器类(以防每个表格视图在其单元格显示逻辑中涉及一些复杂性)。将它们添加为[self addChildViewController:(Your Controller Class),然后在下一行添加[self.view addSubview:(Your Controller Class' view)],并将视图调整为您要设置的框架。

            【讨论】:

              【解决方案6】:

              【讨论】:

                【解决方案7】:

                在所有委托和数据源方法中,第一个参数是对 tableview 对象的引用。因此,您始终可以区分您的表格视图。

                【讨论】:

                  【解决方案8】:

                  ViewController.h

                  {
                      NSArray *arr1;
                      NSArray *arr2;
                      NSArray *arr3;
                  }
                  @property (nonatomic, retain) IBOutlet UITableView *tbl1;
                  @property (nonatomic, retain) IBOutlet UITableView *tbl2;
                  @property (nonatomic, retain) IBOutlet UITableView *tbl3;
                  

                  不要忘记将此属性与XIB's UITableView 实例连接。

                  ViewController.m

                  @synthesize tbl1, tbl2, tbl3;
                  
                  - (NSInteger)tableView:(UITableView *)tableView 
                   numberOfRowsInSection:(NSInteger)section {
                      if (tableView == tbl1)
                          return [arr1 count];
                      if (tableView == tbl2)
                          return [arr2 count];
                      if (tableView == tbl3)
                          return [arr3 count];
                      return 0;
                  }
                  

                  希望对你有所帮助。

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2011-06-26
                    • 1970-01-01
                    • 1970-01-01
                    • 2021-03-12
                    相关资源
                    最近更新 更多