在单个xib中有两个表视图,一个是纯色样式,另一个是分组样式
问题描述:
我想在分段控件下使用两个表视图。一个是普通的样式,另一个是组合的。我如何控制两个表视图的委托和数据源?在单个xib中有两个表视图,一个是纯色样式,另一个是分组样式
- 问候, 赛义德·优素福
答
既然你不能改变曾经创造一个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实例的数据源和委托更改为任何您想要的内容,这可能会帮助您使用单独的控制器封装不同的数据源。
希望这会有所帮助!
答
在保存UI视图控制器中使用多个或两个独占表共享相同的数据源。
我面对这样的问题,如果有人需要以后... 我尝试将我的ViewController设置为两个不同表的数据源。但它没有奏效。视图加载时仅显示2个表格。根据标志,任何一个都将隐藏在viewDidLoad中。似乎一次数据源被调用一个表,它不会被调用第二个表。所有连接均在IB中设置。
解决的办法是在viewDidLoad中设置代码中的dataSource。然后它工作。
-(void) viewDidLoad(){
table1.dataSource = self;
table2.dataSource = self;
if(someCondition == YES)
table1.visible = NO;
else
table2.visible = NO;
}
答
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.