【问题标题】:iOS program to use multiple UITableView in a single UIViewControlleriOS 程序在单个 UIViewController 中使用多个 UITableView
【发布时间】:2013-11-12 11:46:19
【问题描述】:

我正在尝试在情节提要的单个 segue 中实现 3 个表。 选择一个表时,它会加上另一个表的视图,同样还有一个。 以下代码用于一个表格,每个表格的单元格格式不同,行也不同。那么如何通过编码为每个表设置不同的行数等来区分每个表?

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  3;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell2";
    UITableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell1==nil)
{
    cell1=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
    temp=[array objectAtIndex:indexPath.row];
    UILabel *Label1 = (UILabel *)[cell1 viewWithTag:4];
    Label1.text = temp.Title;
    UILabel *Label2 = (UILabel *)[cell1 viewWithTag:6];
    Label2.text = temp.Title;
    UITextField *textfield1 = (UITextField *)[cell1 viewWithTag:5];
    textfield1.text =temp.description;
    UILabel *Label3 = (UILabel *)[cell1 viewWithTag:7];
    Label3.text = temp.Title;
    return cell1;
}
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.showlist=[[ShowList alloc]initWithNibName:@"ShowList" bundle:nil];
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    ShowlistIndex=indexPath.row;
    _secondview.hidden=NO;
}

【问题讨论】:

  • 与其将所有UITableViews 放入一个UIViewController,不如创建一个UIViewController 和3 个UITableViewControllers。添加每个UITableViewController 作为UITableViewController 的子视图控制器。这允许您单独实现每个UITableViewController,而无需在每个表视图委托方法中使用一堆 if/else if。
  • @bobnoble 我想到了这一点,但我在实现子视图控制器时遇到了一些问题。以前没做过。这就是为什么我试图把它放在子视图中。无论如何,谢谢你的观点。

标签: ios uitableview uiviewcontroller multiple-tables


【解决方案1】:

您应该在.h 文件中声明您的tableViews

@property (weak, nonatomic) UITableView *firstTableView;
@property (weak, nonatomic) UITableView *secondTableView;
@property (weak, nonatomic) UITableView *thirdTableView;

然后所有的委托方法都有变量指向女巫对象调用这个方法,所以你可以检查:

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(tableView == self.firstTableView)
       return  3;
   else if(tableView == self.secondTableView)
       return 4;
   else if(tableView == self.thirdTableView)
      return 100;
}

其他委托方法的工作方式相同。

【讨论】:

    【解决方案2】:

    您可以使用类属性来跟踪不同的表格视图,例如:

    @property (nonatomic, strong) UITableView *tableView1;
    @property (nonatomic, strong) UITableView *tableView2;
    @property (nonatomic, strong) UITableView *tableView3;
    

    在委托方法中,您可以检查正确的 tableView 例如:

    if (tableView == self.tableView1) {
        // add code for tableView1
    } else if (tableView == self.tableView2) {
        // add code for tableView2
    } else if (tableView == self.tableView3) {
        // add code for tableView3
    } else {
        // unknown tableView
    }
    

    【讨论】:

      【解决方案3】:

      您在每个委托方法中都有 tableview 引用,对吗?您可以根据它找出您当前正在浏览的 tableview..

      假设..

      IBOutlet UITableView *tableView1;
      IBOutlet UITableView *tableView1;
      IBOutlet UITableView *tableView1;
      

      例如:

      -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
      {
          if(tableView == tableView1)
            return 1;
          if(tableView == tableView2)
            return 5;
          return 10;
      }
      

      您可以对其他委托方法执行相同的操作.. 我希望我正确理解了你的问题..

      【讨论】:

        【解决方案4】:

        您需要创建 3 个 uitableview 网点。然后您可以通过指定标签来识别表格视图。例如 tableview1.tag=1、tableview.tag=2 等

        然后在 tableview 方法中你可以使用它。例如。

        -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
        {
         if(tableView.tag==1){
        return  3;
        }
        else if(tableView=2)
        {return 3;}
        }
        

        可能是这个帮助。

        【讨论】:

          【解决方案5】:

          斯威夫特 4.1

          4.1 是我编写代码的版本。它可能适用于其他版本的 swift。我不确定。


          首先,右击tableViewContent1和tableViewContent2并拖到dataSource和 如下图所示,将委托给 View Controller


          其次,你可以按照我下面的代码来实现。 * 我已经在 tableViewContent1 和 tableViewContent2 中设置了标签 = 1000 。


          导入 UIKit

          class ViewController: UIViewController {
          
              @IBOutlet weak var tableViewContent1: UITableView!
              @IBOutlet weak var tableViewContent2: UITableView!
          
              override func viewDidLoad() {
                  super.viewDidLoad()
          
              }
          
          }
          

          extension ViewController :
          UITableViewDelegate,UITableViewDataSource {
          
          
              func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
                  if tableView == tableViewContent1 {
                      // in order to make the tableViewContent1 get 5 rows.
                      return 5
                  }
                  else{
                      // in order to make the tableViewContent2 get 3 rows.
                      return 3
                  }
          
              }
          
              func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
          
                  if tableView == self.tableViewContent1 {
                      let cell = tableView.dequeueReusableCell(withIdentifier: "content1", for: indexPath) as! TableViewCellFirstCustom 
                      let label : UILabel = cell.viewWithTag(1000) as! UILabel{
                          lab.text = "to change label in content1."
                      }
                      return cell
                  }
          
                  else {
                      let cell = tableView.dequeueReusableCell(withIdentifier: "content2", for: indexPath)  
                      let label : UILabel = cell.viewWithTag(1000) as! UILabel
                      label.text = "to change label in content2."
                      return cell
                  }
              }
          
          }
          

          之后,您将获得如下图所示的结果。 它对我有用。

          【讨论】:

            猜你喜欢
            • 2014-03-02
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-11-02
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-09-15
            相关资源
            最近更新 更多