Two UITableView in the same view

2020-01-31 02:03发布

I want to know if it's allowed to use Multiple UItableView in the same View (i don't see any thing in the Apple's Human Interface Guidelines) and if it's OK, How to load different DataSource in viewDidLoad for each UITableView?

6条回答
我命由我不由天
2楼-- · 2020-01-31 02:13

You can have multiple table views in a single view. Add tags to each table view and with use of tableview.tag you can load data into tableviews separately.

Example:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
     if (tableView.tag == x) {
        //code to load table view with tag value x

        }
        else{
        //code to load second table
        }
        return cell;

    }
查看更多
萌系小妹纸
3楼-- · 2020-01-31 02:16

Yes you can. The issue is that each UITableView will use the same UITableViewDataSource and UITableViewDelegate. Therefore you must determine which table view you are working with in each of the necessary delegate methods.

For example:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    // make bigger rows

    if (tableView == myBigRowTableView)
    {
        // make bigger rows
        return 127;
    } else if (tableView == mySmallRowTableView) {
        // make smaller rows
        return 20;
    } else {
        return 30;
    } 
}
查看更多
在下西门庆
4楼-- · 2020-01-31 02:20

You can most certainly have multiple table views. You would want to make sure you keep a pointer around to each one, then in your data source methods, you would do something like this:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
     if(tableView == tableViewOne)
           return 5;
     else //if (tableView == tableViewTwo)
           return 3;
}

This would be the same for all delegate / data source methods, which is why they give you which table view as a parameter.

查看更多
别忘想泡老子
5楼-- · 2020-01-31 02:20

You can set tag for each table. Then apply on that condition in tableview delegate method, for example:

myTable.tag=12;
查看更多
干净又极端
6楼-- · 2020-01-31 02:23

To make life easier, you can pass in two different delegate to the UITableView. If you pass in the same delegate, you will have to do a lot of if statements. By creating two different delegate it will allow your code to be a lot cleaner.

查看更多
Lonely孤独者°
7楼-- · 2020-01-31 02:29

IMO the cleanest solution would be to have one controller for each tableview.

If you use one controller for n tableview, you will have to use if-statemenst in many places, in

  • – numberOfSectionsInTableView:
  • – tableView:numberOfRowsInSection:
  • – tableView:titleForHeaderInSection:

Basically in all UITableViewDatasource-Protocol methods that you will need to implement.

So if you need to change something, you have to change it in many places.

If you use one controller class for one tableview, you won't have to check at all.

  1. write a controller class for every tableview, make it conforming to the UITableViewDatasource protocol
    • implement the protocol methods you will need. at least
      • – numberOfSectionsInTableView:,
      • – tableView:numberOfRowsInSection:,
      • – tableView:cellForRowAtIndexPath:
  2. call -setDataSource:for every tableview to an object of the right controller class

I wrote an example code: https://github.com/vikingosegundo/my-programming-examples/tree/master/TwoTableViews

TwoTableViewsViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];
    if (firstController == nil) {
        firstController = [[FirstTVContoller alloc] init];
    }
    if (secondController == nil) {
        secondController = [[SecondTVController alloc] init];
    }
    [firstTable setDataSource:firstController];
    [secondTable setDataSource:secondController];

    [firstTable setDelegate:firstController];
    [secondTable setDelegate:secondController];
    firstController.view = firstController.tableView;
    secondController.view = secondController.tableView;
}
查看更多
登录 后发表回答