开发者

having two table views in a single xib one of plain style and the other grouped styled

I want to use two tableviews under the segmentedcontrol. one is plain styled and the other is groupstyled. How can i controll the del开发者_StackOverflow中文版egate and datasource for the two table views?

-- Regards, Syed yusuf


HI,

  I have to inserted the data into table view by using sqlite.But now i need that data in to two table views in next view..I have arranged segmented bar.Am getting two table views but the values in r not displaying.It is displaying NULL.


Using multiple or two exclusive table in the save UI view controller sharing the same data source.

I face a problem like this, if anybody needs later... I try to set my ViewController as a datasource for two different table. But it did not work. 2 tables are exclusively shown at loadtime of the view. Either one will be hidden in viewDidLoad depending on a flag. Seems once dataSource is called for one table, it's not called for the 2nd table. All connections are set in IB.

The solution is to set the dataSource in code in viewDidLoad. Then it works.

    -(void) viewDidLoad(){ 
            table1.dataSource = self; 
            table2.dataSource = self;

            if(someCondition == YES)    
               table1.visible = NO; 
            else     
               table2.visible = NO; 
      }


Since you can't change the UITableViewStyle of a UITableView once created (it can only be set at construction time), you have to have two different instances. You can do this in very different ways, but I've done it this way: add a UISegmentedControl to your interface, and set its target to the RootViewController class instance in your application. The RootViewController class could look like this:

@class DataSource;

@interface RootViewController : UITableViewController 
{
@private
    UITableView *_originalTableView;
    UITableView *_secondaryTableView;
    DataSource *_dataSource;
    BOOL _showingSecondaryTableView;
}

- (IBAction)swap:(id)sender;

@end

And this might be the implementation:

#import "RootViewController.h"
#import "DataSource.h"

@implementation RootViewController

- (void)dealloc 
{
    [_dataSource release];
    [_originalTableView release];
    [_secondaryTableView release];
    [super dealloc];
}

- (void)viewDidLoad 
{
    [super viewDidLoad];

    _dataSource = [[DataSource alloc] init];

    _secondaryTableView = [[UITableView alloc] initWithFrame:self.tableView.frame 
                                                       style:UITableViewStyleGrouped];
    _secondaryTableView.delegate = _dataSource;
    _secondaryTableView.dataSource = _dataSource;

    _originalTableView = [self.tableView retain];

    _showingSecondaryTableView = NO;
}

- (void)didReceiveMemoryWarning 
{
    [super didReceiveMemoryWarning];
}

#pragma mark -
#pragma mark IBAction method

- (IBAction)swap:(id)sender
{
    if (_showingSecondaryTableView)
    {
        self.tableView = _originalTableView;
        _showingSecondaryTableView = NO;
    }
    else
    {
        self.tableView = _secondaryTableView;
        _showingSecondaryTableView = YES;
    }
}

#pragma mark -
#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView 
 numberOfRowsInSection:(NSInteger)section 
{
    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
                                       reuseIdentifier:CellIdentifier] autorelease];
    }

    cell.textLabel.text = [NSString stringWithFormat:@"RootViewController cell %d", indexPath.row];
    return cell;
}

@end

This is the interface of the DataSource class:

#import <UIKit/UIKit.h>

@interface DataSource : NSObject <UITableViewDelegate, 
                                  UITableViewDataSource>
{
}

@end

And the implementation:

#import "DataSource.h"

@implementation DataSource

#pragma mark -
#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView 
 numberOfRowsInSection:(NSInteger)section 
{
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{

    static NSString *CellIdentifier = @"DataSourceCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
                                       reuseIdentifier:CellIdentifier] autorelease];
    }

    cell.textLabel.text = [NSString stringWithFormat:@"DataSource cell %d", indexPath.row];
    return cell;
}

@end

You can change the datasource and delegate of the UITableView instances to anything you want, at any time during runtime, which might help you encapsulate different data sources with separate controllers.

Hope this helps!

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜