0

私はiOSプログラミングに不慣れで、このチュートリアルを読んでhttp://www.raywenderlich.com/1797/how-to-create-a-simple-iphone-app-tutorial-part-1を理解しています。

MasterViewController.mを開いてこれを行うように指示する部分があります。

// Replace the return statement in tableView:numberOfRowsInSection with the following:
return _bugs.count;

この:

// Replace tableView:cellForRowAtIndexPath with the following
- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView
                             dequeueReusableCellWithIdentifier:@"MyBasicCell"];
    ScaryBugDoc *bug = [self.bugs objectAtIndex:indexPath.row];
    cell.textLabel.text = bug.data.title;
    cell.imageView.image = bug.thumbImage;
    return cell;
}

ただし、このクラスには、「tableView:numberOfRowsInSection」や「tableView:cellForRowAtIndexPath」などのステートメントは見つかりません。

これが私のMasterViewController.mクラスのコードです:

#import "ScaryBugsMasterViewController.h"
#import "ScaryBugDoc.h"
#import "ScaryBugData.h"

@implementation ScaryBugsMasterViewController

@synthesize bugs = _bugs;


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

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.title = @"Scary Bugs"; 
}

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

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return YES;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source.
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

@end

クラスを作成するときに何か間違ったことをしましたか?

ありがとうございました。

4

2 に答える 2

2

UITableViewが機能するには、これら3つのメソッドが必要です。

(1)表のセクション数を指定します

(2)テーブルのセクションごとの行数を指定します

(3)テーブルの行を埋めるUITableViewCellを作成します

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section
{
    return [self.bugs count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView
                             dequeueReusableCellWithIdentifier:@"MyBasicCell"];
    ScaryBugDoc *bug = [self.bugs objectAtIndex:indexPath.row];
    cell.textLabel.text = bug.data.title;
    cell.imageView.image = bug.thumbImage;
    return cell;
}

編集:ページの下部に移動し、ソースコードをダウンロードします。これらのメソッドはMasterViewControllerに実装されています。

新しいクラスを作成するときは、UITableViewControllerをサブクラス化するだけで、これらのメソッドテンプレートは自動的にクラスに含まれるため、実装する必要があります。

于 2012-05-09T01:06:57.873 に答える
2

UITableViewControllerの代わりにUIViewControllerを使用しているようです。UITableViewControllerを使用すると、テーブルビューデリゲートメソッドとデータソースメソッドのスタブを取得できます。

于 2012-05-09T01:08:02.917 に答える