开发者

2 diff UITableViews on a single UIView

Is it possible to have 2 different UITableViews added on a single UIView, with data source and delegate of both the tableViews being handled inside the UI开发者_Python百科View class file itself ?


Yes. Notice the 1st argument of all the delegate/datasource methods. It is the reference of the tableView being handled.

It can be done, but depending on the situation, there might be more elegant ways of doing whatever you are trying to do.


First off, you should probably not put the delegate code in the UIView class file itself in keeping with MVC patterns. Consider having a UIViewController handle that instead, and leave the UIView to handle only things concerning how and where things get drawn.

Other than that, it is possible. You set both UITableView's delegates to the be the superview's controller. Since all delegate prototypes require the UITableView to be passed in as an argument, you can simply verify which of the UITableView's is calling the method by using this passed in pointer and then execute that UITableView's code.

You can keep store the UITableView pointers as IVars in your controller class to look them up later.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == myFirstTableView) {
        return myFirstTableViewHeight;
    else if (tableView == mySecondTableView) {
        return mySecondTableViewHeight;
    }

    return 0; 
}


Yes it is. But you have to invest some work to have this build up cleanly.

First I would suggest to build a Controller object which has two UITableViewController objects as properties.

These will manage the table views.

You can assign a datasource and a delegate to each of the table view controllers.

@interface TableViewManager : NSObject { UITableViewController *myTableViewController1; UITableViewController *myTableViewController2; }

@property(nonatomic, retain) UITableViewController *myTableViewController1; @property(nonatomic, retain) UITableViewController *myTableViewController2;

// These will be implemented manually to delegate to the corresponding TableVC @property (nonatomic, assign) id myTableViewDelegate1; @property (nonatomic, assign) id myTableViewDelegate2; @property (nonatomic, assign) id myTableViewDataSource1; @property (nonatomic, assign) id myTableViewDataSource2;

@end

@implementation

@synthesize myTableViewController1, myTableViewController2;

  • (id) init { if ( (self = [super init]) ) { // actually you can take these lines to a nib by defining a UITableViewController like object. self.myTableViewController1 = [UITableViewController alloc] initWithStyle:ITableViewStylePlain]; self.myTableViewController1 = [UITableViewController alloc] initWithStyle:ITableViewStylePlain]; }

    return self; }

// Example for 1 setter, the rest of the assign-properties will be similar - (void) setMyTableViewDelegate1:(id)aDelegate { self.myTableViewController1.delegate = aDelegate; }

  • (void) dealloc { self.myTableViewDelegate1 = nil; self.myTableViewDelegate2 = nil; self.myTableViewDataSource1 = nil; self.myTableViewDataSource2 = nil;

    self.myTableViewController1 = nil; self.myTableViewController2 = nil;

    [super dealloc]; }

@end

After that class has been initialized you can assign the tableViews from each table view controller to your UIView by applying the addSubview: message.

You have the benefit of a clean structure and no problems with bloated classes.

If you want and/or need it you may define your own custom class of UITableViewController and modify it to your needs, for example that it uses a custom derivative of UITableView.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜