UITableView numberOfRowsInSection 不会加载多于一行 [英] UITableView numberOfRowsInSection Will Not Load More than One Row

查看:31
本文介绍了UITableView numberOfRowsInSection 不会加载多于一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个 UITableView,它应该遍历单个 NSMutableArray 并将它们中的每一个用作行标签.目前,我可以让它运行的唯一方法是使用 0 或 1 行,2 或更高的行会抛出一个错误,指出数组索引已关闭.我尝试使用 NSLog 来输出我的数组,并且可以确认它正在读取所有字符串.

So I have a UITableView which should loop through a single NSMutableArray and use each of them as row labels. Currently the only way I can get this to run is with 0 or 1 rows, 2 or higher throws out an error saying the array index is off. I tried NSLog to output my array and can confirm it's reading all the Strings.

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}

// Set up the cell...
NSString *cellValue = [harvestRecipeList objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;

return cell;
} 

数组代码存储在我在下面添加的完全相同的文件 (MasterViewController.m) 中.

The array code is stored in the exact same file (MasterViewController.m) which I added below.

- (void)viewDidLoad
{
    [super viewDidLoad];

    harvestRecipeList = [[NSMutableArray alloc] init];

    [harvestRecipeList addObject:@"Ice Cream"];
    [harvestRecipeList addObject:@"Walnut Cake"];
    [harvestRecipeList addObject:@"Cookies"];
    [harvestRecipeList addObject:@"Salad"];
    [harvestRecipeList addObject:@"Grilled Fish"];

    //Set the title
    self.navigationItem.title = @"BTN Recipes";
}

我希望得到任何帮助,这一直困扰着我.我正在使用 [harvestRecipeList count] 但这会引发相同的数组索引错误.正如我所提到的,我可以让应用在 0 行或 1 行的情况下完美运行 - 在此先感谢您的帮助!

I would love any help on this it's been bugging me. I was using [harvestRecipeList count] but this throws the same array index error. And as I mentioned I can get the app to run perfectly fine with 0 or 1 rows - thanks in advance for any help!

这是我在构建后在输出窗口中遇到的错误:

here is the error I'm getting in the output window after building:

Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'

EDIT2:包含在我为收获食谱列表设置的属性下方

EDIT2: included below my property setup for harvestRecipeList

//MasterViewController.h
@interface MasterViewController : UITableViewController {
    NSMutableArray *harvestRecipeList;
}
@property (nonatomic, retain) NSMutableArray *harvestRecipeList;

// and also my MasterViewController.m 
@synthesize harvestRecipeList;

EDIT3这是我为此项目压缩的源代码.它被称为树屋,目前只是一个测试名称,但您可以此处从我的云应用程序 dl.

EDIT3 here's my source code zipped for this project. It's called treehouse, just a testing name for now but you can dl from my cloudapp here.

推荐答案

我检查了您放入 ZIP 文件中的代码.我立即注意到您使用了 iOS 5 Storyboard 功能.我没有 Xcode 4 或 iOS 5 SDK,所以我无法测试您应用程序的那部分.

I examined the code you put in the ZIP file. I immediately noticed your using the iOS 5 Storyboard feature. I haven't got Xcode 4 nor the iOS 5 SDK, so I could not test that part of your application.

但是,我继续手工编写了您的故事板部分.我已经单独测试了您的 MasterViewController 并没有发现任何错误.我在 AppDelegate 中添加了此方法来替换 Storyboard 自动功能,并仅显示您认为错误来自何处的视图控制器.

However, I went on and coded your Storyboard part by hand. I have tested your MasterViewController solely and found no errors. I added in the AppDelegate this method to replace the Storyboard automatical features and just show the view controller where you think the error is coming from.

- (BOOL)application:(UIApplication *)application 
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    MasterViewController *myVC;
    _window = [[UIWindow alloc] initWithFrame:
               [[UIScreen mainScreen] applicationFrame]];
    myVC = [[MasterViewController alloc] initWithStyle:UITableViewStylePlain];
    [_window setAutoresizesSubviews:YES];
    [_window addSubview:myVC.view];

    [_window makeKeyAndVisible];
    return YES;
}

为了证明你的 MasterViewController.m 没有错误,我添加了这个截图:

To prove your MasterViewController.m contains no error, I add this screenshot:

结论:您的错误可以在其他地方找到,可能在您的 Storyboard 文件中.但是,我从未使用过该新功能,因此我无法为您提供帮助.我建议您查看您的故事板并将所有注意力集中在该文件上.

Conclusion: Your error is to be found somewhere else, probably in your Storyboard file. However I have never used that new functionality so I cannot help you with that. I suggest you review your Storyboard and put all attention to that file.

这篇关于UITableView numberOfRowsInSection 不会加载多于一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆