2 UITableViews in one UIView

后端 未结 5 1350
一生所求
一生所求 2020-12-02 23:23

I have a UIView that will need to display two UITableViews, but they are never shown together, by using a SegementedBar you can toggle one or the o

相关标签:
5条回答
  • 2020-12-02 23:48

    I would keep one datasource & delegate.

    This means that all the delegate/datasource methods become more complicated BUT it means that you can retain the one to one relationship between viewController & view.

    keep a reference to each of the table views

    //vc.h
    @property (nonatomic, weak) IBOutlet UITableView* firstTableView;
    @property (nonatomic, weak) IBOutlet UITableView* secondTableView;
    

    In the datasource/ delegate methods you need to account for the fact that the method needs to behave differently depending on which table view is in use. e.g.

    //vc.m
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        ...
    
        if (tableView == self.firstTableView) {
    
            ...
    
        } else { // tableView == self.secondTableView
    
            ...
        }
    }
    
    return cell;
    

    }

    0 讨论(0)
  • 2020-12-02 23:51

    In my current app, I need to have 4 UITableView in a single UIViewController, at once I've to show single table, based on the tab selected by the user, I've added four tables because, all of having different custom cells and functionality, to reduce complexity I took four.

    The main benefit of this is that, each time you don't need to call reloadData to update a single table. I just need to properly handle table's show & hide flow. And believe me that's looks cool. Not flicking at all.

    In my case, I am creating four tables by code only. And I make a method that will return me a table based upon a tag I've pass.

    I keep cellForRowAtIndexPath as small as possible by dividing code into different functions.

    0 讨论(0)
  • 2020-12-02 23:53
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        ...
    
        if (tableView.tag == 1) {
    
            ...
    
        } else { // tableView == self.secondTableView
    
            ...
        }
    }
    

    tag could be assigned from the .xib. so no need to have UITableVeiw variable in .h file. Two table view in .xib needed

    0 讨论(0)
  • 2020-12-02 23:53

    Use separate UITableViewControllers and swap the views. It's less code, less complexity and it's the way Apple does it with the TabBar.

    As for code complexity, there really isn't any. You simply do the following to switch views when the UISegmentedControl's value has changed:

    UIView *previousSuperview = myViewController1.view.superview;
    myViewController2.view.frame = myViewController1.view.frame;
    [myViewController1.view removeFromSuperview];
    [previousSuperview addSubview:myViewController2.view];
    

    Alternatively, you could set the corresponding view's hidden property.

    0 讨论(0)
  • 2020-12-02 23:57

    Both approach has some pros and cons, but i will personally prefer approach having two separate controller.

    Approach 1 - create one Table View Controller and change the data source

    • This approach help in avoiding extra and repeated code.
    • With this memory management is good as using one controller only.(Although this is not a big concern till then we won't have a lot of data.)
    • Issue with this is having complexity.

    Approach 2 - 2 Table View Controller

    • With this approach definitely have extra and repeated code.
    • But with this is less complexity.
    0 讨论(0)
提交回复
热议问题