Iphone编程:多个UITableViews从同一个源读取?

时间:2010-11-08 09:56:30

标签: iphone xcode uitableview datasource multiple-tables

这个问题与UITableView issue when using separate delegate/dataSource有关,但我遇到了不同的问题。我刚刚开始学习iPhone编程。

基本上我有一个带有表格的主视图。单击单元格时,将显示带有另一个表的子视图。

主视图表的数据源和委托被设置为文件的所有者,我在那里添加了必要的代码来处理表数据,一切都很好。 但是,当子视图中的第二个表似乎使应用程序崩溃时,我做了同样的事情,设置数据源并委托给文件的所有者,并重复与主视图表相同的过程。我不知道为什么会这样。

子视图有唯一的nib / xib文件和自己的插件。如果我没有将任何数据源附加到子视图的表中,它将从主视图的表中获取数据;我不明白为什么会这样,因为我已经将数据源设置为文件的所有者。

例如:FirstView控制器有一个表FirstTable,数据源和委托设置为Files的所有者。我在FirstView.m中添加了以下内容:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 4;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"LibraryListingCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                       reuseIdentifier:CellIdentifier] autorelease];
    }
    cell.textLabel.text =@"Table Cell";
    return cell;
}

一切都很完美。 当我用第二个表和第二个视图重复此操作时,应用程序崩溃说

reason: '-[UISectionRowData tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x69188d0'

我对第二个表完全相同:在numberOfRowsInSection内实现了cellForRowAtIndexPatchsecondview.m,并将第二个表的委托和数据源设置为文件的所有者。如果我删除第二个表的委托和数据源,应用程序不会崩溃,但在第二个视图中有一个空表。

有什么建议吗?或者我在这里错过了一些关键概念?

3 个答案:

答案 0 :(得分:3)

您可以为多个表使用相同的数据源和委托方法。 你必须提到你在哪个表中进行操作。 例如:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([tableView isEqual:TableView1])
    {
       //do work for tableview1
     }
     else if([tableView isEqual:TableView2])
   {
      //do operations for tableview2
   }
}

答案 1 :(得分:1)

这是主要的View控制器.h文件。

#import <UIKit/UIKit.h>
#import "SubView.h"
@interface StackOverTableSubViewViewController : UIViewController <UITableViewDelegate,UITableViewDataSource>
{
    UIView *contentView;
    UITableView *tblVw;
    NSMutableArray *array;
    SubView *SubViewObj;
}
@property(nonatomic,retain) UIView *contentView;
@property(nonatomic,retain) UITableView *tblVw;
@property(nonatomic,retain) NSMutableArray *array;
@property(nonatomic,retain) SubView *SubViewObj;
@end

这是主要的View控制器.m文件。

#import "StackOverTableSubViewViewController.h"
@implementation StackOverTableSubViewViewController
@synthesize contentView,tblVw,array,SubViewObj;

// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
    contentView=[[UIView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    contentView.autoresizingMask=(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
    contentView.autoresizesSubviews=YES;
    contentView.backgroundColor=[UIColor whiteColor];

    tblVw=[[UITableView alloc]initWithFrame:[[UIScreen mainScreen]bounds] style:UITableViewStylePlain];
    tblVw.dataSource=self;
    tblVw.delegate=self;
    tblVw.scrollEnabled=YES;

    array=[[NSMutableArray alloc]init];
    [array addObject:@"Row1"];
    [array addObject:@"Row2"];
    [array addObject:@"Row3"];
    [array addObject:@"Row4"];
    [array addObject:@"Row5"];
    [array addObject:@"Row6"];
    [array addObject:@"Row7"];
    [array addObject:@"Row8"];
    [array addObject:@"Row9"];
    [array addObject:@"Row10"];
    [array addObject:@"Row11"];
    [array addObject:@"Row12"];
    [contentView addSubview:tblVw];
    self.view=contentView;
}

- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
    return [array count];
}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CellIdentifier";

    // Dequeue or create a cell of the appropriate type.
    // UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
        cell.textLabel.numberOfLines = 0;
        [cell.textLabel sizeToFit];
    }
    cell.textLabel.text=[array objectAtIndex:indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SubViewObj=[[SubView alloc]init];
    [self.view addSubview:SubViewObj.view];
}

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)dealloc {
    [contentView release];
    [SubViewObj release];
    [tblVw release];
    [array release];
    [super dealloc];
}

@end

添加一个名为subview的视图控制器。这是subview.h

#import <UIKit/UIKit.h>
@interface SubView : UIViewController <UITableViewDelegate,UITableViewDataSource>
{
    UIView *contentView;
    UITableView *tblVw;
    NSMutableArray *array;
}
@property(nonatomic,retain) UIView *contentView;
@property(nonatomic,retain) UITableView *tblVw;
@property(nonatomic,retain) NSMutableArray *array;
@end

subview.m:     #import“SubView.h”     #进口     @implementation SubView     @synthesize contentView,tblVw,array;

// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
    contentView=[[UIView alloc]initWithFrame:CGRectMake(200, 10, 300, 600)];
    contentView.autoresizingMask=(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
    contentView.autoresizesSubviews=YES;
    contentView.backgroundColor=[UIColor whiteColor];

    tblVw=[[UITableView alloc]initWithFrame:CGRectMake(200, 10, 300, 600) style:UITableViewStylePlain];
    tblVw.dataSource=self;
    tblVw.delegate=self;
    tblVw.scrollEnabled=YES;
    tblVw.layer.borderWidth=4.0;
    tblVw.layer.borderColor=[[UIColor redColor]CGColor];
    array=[[NSMutableArray alloc]init];
    [array addObject:@"Data1"];
    [array addObject:@"Data2"];
    [array addObject:@"Data3"];
    [array addObject:@"Data4"];
    [array addObject:@"Data5"];
    [array addObject:@"Data6"];
    [array addObject:@"Data7"];
    [array addObject:@"Data8"];
    [array addObject:@"Data9"];
    [array addObject:@"Data10"];
    [array addObject:@"Data11"];
    [array addObject:@"Data12"];

    [contentView addSubview:tblVw];
    self.view=contentView;

}

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/

- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
    return [array count];
}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CellIdentifier";

    // Dequeue or create a cell of the appropriate type.
    // UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
        cell.textLabel.numberOfLines = 0;
        [cell.textLabel sizeToFit];
    }
    cell.textLabel.text=[array objectAtIndex:indexPath.row];
    return cell;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Overriden to allow any orientation.
    return YES;
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

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

@end

试试这段代码。这个应用程序是为iPad完成的。根据iPhone的需要更改尺寸。

答案 2 :(得分:1)

我有完全相同的问题并通过从表视图的数据源删除我的连接到文件所有者来修复它。然后我粘贴在下面的代码中替换现有的cellForRowAtIndexPath。我不得不更改数组名称以匹配我的数组,然后将数据源重新连接到文件所有者,它开始工作。一定是我的功能代码中的一个snafu。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CellIdentifier";
    // Dequeue or create a cell of the appropriate type.
    // UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
        cell.textLabel.numberOfLines = 0;
        [cell.textLabel sizeToFit];
    }
    cell.textLabel.text=[array objectAtIndex:indexPath.row];
    return cell;
}