1

这个问题与使用单独的委托/数据源时的 UITableView 问题有关,尽管我有一个不同的问题。我刚开始学习 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'

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

有什么建议么?还是我在这里遗漏了一些关键概念?

4

3 回答 3

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
   }
}
于 2010-11-08T12:47:33.420 回答
1

这是主视图控制器.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

这是主视图控制器.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" #import @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 的需要更改尺寸。

于 2010-11-10T07:51:13.227 回答
1

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

- (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;
}
于 2011-08-31T21:48:48.673 回答