我想在segmentedcontrol下使用两个tableviews。一个是简单的风格,另一个是有组织的。如何控制两个表视图的委托和数据源?
- 问候, Syed yusuf
答案 0 :(得分:1)
在保存UI视图控制器中使用多个或两个独占表,共享相同的数据源。
我遇到这样的问题,如果以后有人需要...... 我尝试将我的ViewController设置为两个不同表的数据源。但它没有用。在视图的加载时间专门显示2个表。根据标志,任何一个都将隐藏在viewDidLoad中。似乎一旦为一个表调用了dataSource,就不会为第二个表调用它。所有连接都在IB中设置。
解决方案是在viewDidLoad中的代码中设置dataSource。然后它工作。
-(void) viewDidLoad(){
table1.dataSource = self;
table2.dataSource = self;
if(someCondition == YES)
table1.visible = NO;
else
table2.visible = NO;
}
答案 1 :(得分:1)
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.
答案 2 :(得分:0)
由于您无法在创建UITableView时更改它的UITableViewStyle(它只能在构造时设置),因此您必须有两个不同的实例。你可以用非常不同的方式做到这一点,但我这样做了:将UISegmentedControl添加到你的接口,并将其目标设置为应用程序中的RootViewController类实例。 RootViewController类可能如下所示:
@class DataSource;
@interface RootViewController : UITableViewController
{
@private
UITableView *_originalTableView;
UITableView *_secondaryTableView;
DataSource *_dataSource;
BOOL _showingSecondaryTableView;
}
- (IBAction)swap:(id)sender;
@end
这可能是实施:
#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
这是DataSource类的接口:
#import <UIKit/UIKit.h>
@interface DataSource : NSObject <UITableViewDelegate,
UITableViewDataSource>
{
}
@end
实施:
#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
您可以在运行时的任何时候将UITableView实例的数据源和委托更改为您想要的任何内容,这可能有助于您使用单独的控制器封装不同的数据源。
希望这有帮助!