Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/105.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS Xcode 4.2主详细信息应用程序模板引发异常_Ios_Ios5_Xcode4.2_Master Detail_Nsrangeexception - Fatal编程技术网

iOS Xcode 4.2主详细信息应用程序模板引发异常

iOS Xcode 4.2主详细信息应用程序模板引发异常,ios,ios5,xcode4.2,master-detail,nsrangeexception,Ios,Ios5,Xcode4.2,Master Detail,Nsrangeexception,新手问题在这里 我已经使用ARC和故事板在Xcode 4.2中创建了一个主细节应用程序项目。我已将模板修改为: MasterViewController.h 当程序运行时,此代码将在此行失败: [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionMiddle]; 除此之外: ***

新手问题在这里

我已经使用ARC和故事板在Xcode 4.2中创建了一个主细节应用程序项目。我已将模板修改为:

MasterViewController.h 当程序运行时,此代码将在此行失败:

[self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionMiddle];
除此之外:

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

如果我将列表(项目)缩减为单个项目,则MasterViewController将无错误地加载。很明显我做错了什么,但我一辈子都搞不清楚是什么。有人愿意为我指出显而易见的问题吗?

该模板包含一个为静态单元格设置的UITableView。它实际上是一个静态细胞。(这就是为什么将数组设为一个项目的长度会影响工作的原因)

但是你似乎不想在这里看到静态内容。因此,您只需进入情节提要,选择UITableView,转到属性检查器,然后将内容类型更改为动态原型

这会让你克服这个问题

编辑

一个稍微相关的问题是,您可能还希望在故事板中使用原型单元。为此,只需将该原型的单元标识符设置为您在tableView中使用的单元标识符:cellforrowatinexpath:

然后省略整个“if(cell==nil)”部分。牢房不会是零

因此,该方法现在将如下所示:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell"; // <-- Make sure this matches what you have in the storyboard

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // Configure the cell.
    cell.textLabel.text = [self.items objectAtIndex:indexPath.row];

    return cell;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{

静态NSString*CellIdentifier=@“Cell”/模板包含一个为静态单元格设置的UITableView。它实际上是一个静态单元格。(这就是为什么将数组设为一个长项会产生某种效果的原因)

但这里似乎不需要静态内容,所以您只需进入故事板,选择UITableView,转到属性检查器,然后将内容类型更改为动态原型

这会让你克服这个问题

编辑

与此相关的一个问题是,您可能还希望在故事板中使用原型单元。为此,只需将该原型的单元标识符设置为您在tableView中使用的单元标识符:CellForRowatineXpath:

然后省略整个“if(cell==nil)”部分。该单元格不会为nil

因此,该方法现在将如下所示:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell"; // <-- Make sure this matches what you have in the storyboard

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // Configure the cell.
    cell.textLabel.text = [self.items objectAtIndex:indexPath.row];

    return cell;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{

静态NSString*CellIdentifier=@“Cell”;//这正是我的问题。谢谢你的帮助!这正是我的问题。谢谢你的帮助!
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell"; // <-- Make sure this matches what you have in the storyboard

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // Configure the cell.
    cell.textLabel.text = [self.items objectAtIndex:indexPath.row];

    return cell;
}