1

この質問は、別の問題がありますが、個別の delegate/dataSource を使用する場合の UITableView の問題に関連しています。iPhoneのプログラミングを学び始めたばかりです。

基本的に、テーブルのあるメイン ビューが 1 つあります。セルをクリックすると、別のテーブルを含むサブビューが表示されます。

メイン ビューのテーブルのデータ ソースとデリゲートはファイルの所有者として設定され、テーブル データを処理するために必要なコードをそこに追加しました。すべて問題ありません。しかし、サブビューの 2 番目のテーブルがアプリケーションをクラッシュさせたように見えるときは、同じことを行い、データソースとデリゲートをファイルの所有者に設定し、メイン ビューのテーブルと同じ手順を繰り返しました。なぜこれが起こっているのか分かりません。

サブビューには、唯一の 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;
}

すべてが完璧に機能します。2 番目のテーブルと 2 番目のビューでこれを繰り返すと、アプリケーションがクラッシュして、

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

2 番目のテーブルについてもまったく同じことを行いました: 実装さnumberOfRowsInSectionれ、2 番目のテーブルのcellForRowAtIndexPatchデリゲートsecondview.mとデータソースをファイルの所有者に設定します。2 番目のテーブルのデリゲートとデータソースを削除すると、アプリケーションはクラッシュしませんが、2 番目のビューに空のテーブルが表示されます。

助言がありますか?または、ここでいくつかの重要な概念がありませんか?

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

まったく同じ問題があり、テーブルビューのデータソースからファイル所有者への接続を削除することで修正しました。次に、既存の を置き換える以下のコードを貼り付けました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 に答える
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 に答える