objective-c - 遵循 iOS 教程时找不到特定代码

标签 objective-c ios xcode

我是 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

也许是我在创建类时做错了什么?

谢谢。

最佳答案

UITableView 需要这三种方法才能工作。

(1) 指定表格的section数

(2) 指定表格中每个section的行数

(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 子类化,这些方法模板将自动包含在该类中,您只需实现它们即可。

关于objective-c - 遵循 iOS 教程时找不到特定代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10508543/

相关文章:

iphone - ASIHTTPRequest:钥匙串(keychain)持久性

ios - iOS 被拒绝后有没有办法请求许可

ios - 角色在 Scenekit 中没有朝正确的方向移动

ios - Swift Action 扩展 NSString 错误?

ios - NSDate 比较显示错误的值?

iOS:本地化的 UILabel 文本未对齐

ios - 为什么上标数字的Unicode字符显示高度不一样?

xcode - 如何使用 xcode 4 编译 openmpi 程序?

ios - 如何将字体永久添加到 Xcode

ios - UIWebview缓存到磁盘