Adding two Table Views to the main View in Xcode

前端 未结 2 1486
天命终不由人
天命终不由人 2021-01-06 19:00

I placed two Table View\'s onto my main.storyboard. I then created two new .swift class files for them: VideoTableViewController and LinkTableViewController. How do I link t

相关标签:
2条回答
  • 2021-01-06 19:18

    If you want 2 tableviews in the same view controller do the following:

    1. Create a UIViewController that is a delegate to

      UITableViewDelegate, UITableViewDataSource
      
    2. Add 2 uitableviews to this view controller in the storyboard
    3. Add 2 IBOutlets for the UITableViews in your ViewController.h file like this:

      @property (nonatomic, weak) IBOutlet UITableView *tableView1;
      @property (nonatomic, weak) IBOutlet UITableView *tableView2;
      
    4. Connect the IBOutlets to these.

    5. Add the delegate functions for the tableview in the .m file somethng like this:

       - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
       {
          // Return the number of rows in the section.
          if(tableView == self.tableView1){
              return whatever1;
          }
      
          if(tableView == self.tableView2){
              return whatever2;
          }
       }
      
    0 讨论(0)
  • 2021-01-06 19:29

    @hackerinheels' proposed solution worked for me in a bit of a different setting: using a UITableView like a radio button group in a View Controller. It didn't work initially because I missed this step in hooking up the delegate and data source in viewDidLoad() of the View Controller:

    tableView.delegate = self
    tableView.dataSource = self
    

    After this step is added, it started working nicely.

    Even though this might be a no-brainer for experienced folks, but for a layman like me, spelling it out can be very helpful.

    As for the solution for 2 TableViews in one View Controller, @hackerinheels' proposed 5 steps + the above change in viewDidLoad(), would work nicely!

    0 讨论(0)
提交回复
热议问题