Objective c UITableView numberOfRowsInSection将不会加载多行
因此,我有一个UITableView,它应该循环通过一个NSMutableArray,并将每个NSMutableArray用作行标签。目前,我唯一能让它运行的方法是使用0或1行,2或更高的行抛出一个错误,表示数组索引已关闭。我尝试了NSLog来输出我的数组,并且可以确认它正在读取所有字符串Objective c UITableView numberOfRowsInSection将不会加载多行,objective-c,xcode,uitableview,Objective C,Xcode,Uitableview,因此,我有一个UITableView,它应该循环通过一个NSMutableArray,并将每个NSMutableArray用作行标签。目前,我唯一能让它运行的方法是使用0或1行,2或更高的行抛出一个错误,表示数组索引已关闭。我尝试了NSLog来输出我的数组,并且可以确认它正在读取所有字符串 // table methods - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } -
// 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)中
- (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行的情况下运行得非常好-提前感谢您的帮助
编辑:以下是生成后我在输出窗口中遇到的错误:
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
EDIT2:包含在harvestRecipeList的我的属性设置下面
//MasterViewController.h
@interface MasterViewController : UITableViewController {
NSMutableArray *harvestRecipeList;
}
@property (nonatomic, retain) NSMutableArray *harvestRecipeList;
// and also my MasterViewController.m
@synthesize harvestRecipeList;
Updated Solution:
EDIT3
这是我为这个项目压缩的源代码。它被称为treehouse,目前只是一个测试名称,但您可以。好的,如果您的日志消息在您尝试查询第二个对象之前显示一个包含五个对象的数组,并且您的应用程序向您提供了一个
NSRangeException
,那么在您显示的代码中肯定找不到该错误
尝试在任何-[NSArray objectAtIndex:
之前和之后放置各种日志来查找它,并查看调用后哪个日志没有通过。这是你的错误
记住你可以使用
NSLog(@"%s", __PRETTY_FUNCTION__);
以显示日志消息的来源。它们还存在一个line和file宏,但通常情况下function宏应该可以为您提供足够的帮助
示例:
NSLog(@"%s Before", __PRETTY_FUNCTION__);
[myArray objectAtIndex:anIndex];
NSLog(@"%s After", __PRETTY_FUNCTION__);
如果第二条日志消息没有通过,那么您将发现错误。最好对实例变量使用setter和getter方法。它解决了很多问题。我猜这是你的问题。因此,您可以在任何地方使用
harvestRecipeList
使用self.harvestRecipeList
了解您的财产声明对于harvestRecipeList来说是非常有用的
//MasterViewController.h
@interface MasterViewController : UITableViewController {
NSMutableArray *harvestRecipeList;
}
@property (nonatomic, retain) NSMutableArray *harvestRecipeList;
// and also my MasterViewController.m
@synthesize harvestRecipeList;
Updated Solution:
所以我检查了你的代码,发现了问题。请执行以下操作:
静态单元格
更改为动态原型
单元格
(这是您正在使用的单元格IDNone
更改为disposition Indicator
表格视图中:numberOfRowsInSection
returnself.harvestRecipeList.count
tableView:cellForRowAtIndexPath:
中,可以删除以下两行(因为它们是由情节提要提供的):
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:CellIdentifier];
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicatorPrevious Solution:
所以,有一些评论,但是,首先,如果你已经更新了你的代码,你能发布一个更新吗
ViewDidLoad
中添加对象的harvestRecipeList
似乎与您合成的harvestRecipeList
不同-它将是方法的局部变量,因此您的实例变量将始终为零。您应该始终使用self.harvestRecipeList
-在任何地方都这样做。这可能会导致请仔细解释您的NSRangeException
。另请参见下面的#4表格视图:numberofrowsin部分:
中,您应该返回self.harvestRecipeList.count
cell==nil
,因为dequeueReusableCellWithIdentifier:
保证返回非nil cell。您需要检查是否在iOS4 SDK上@synthesis-harvestRecipeList;
更改为@synthesis-harvestRecipeList=\u-harvestRecipeList;
,因为这将有助于#1并检查您是否正在访问ivar请至少尝试
#1
和#2
,然后发布您遇到的问题的更新。希望这会有所帮助。我检查了您放在ZIP文件中的代码。我立即注意到您正在使用iOS 5情节提要功能。我没有Xcode 4或iOS 5 SDK,因此无法测试应用程序的这一部分。>
Previous Solution:
但是,我继续手动编写了您的故事板部分。我单独测试了您的MasterViewController
,没有发现任何错误。我在AppDelegate
中添加了此方法以替换故事板自动功能,并仅显示您认为错误来自的视图控制器
- (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
没有错误,我添加了以下屏幕截图:
结论:您的错误可能会在其他地方找到,可能是在您的故事板文件中。但是,我从未使用过该新功能,因此我无法帮助您。我建议您查看您的故事板,并将所有注意力放在该文件上。请公布您收到的错误。如果发送,肯定有问题g-count
到NSMutableArray
的一个实例正在抛出错误。在查询对象之前,您是否尝试过NSLog
-ing您的harvestRecipeList
以确保它包含正确的元素?@MarkAdams yes sure th