繁体   English   中英

为什么我在Xcode中得到NSRangeException

[英]why am I getting NSRangeException in Xcode

我正在尝试从我的项目中加载一个plist,这一直有效,直到我不小心删除了我的plist。 plist有5个数组,每个数组2个元素。 我知道程序正在尝试访问数组范围之外的内容,但是我不知道该索引的位置是什么? 这是它轰炸的代码:该代码成功执行了两次,然后由于某种原因试图再次访问它,并在第一行轰炸,为什么?

它引发以下异常:

NSRangeException -[_NSCFARRAY objectAtIndex] index(2) beyond bounds (2)

请帮忙,这是定于周一完成的最终项目,现在我觉得我必须重新开始。

 NSString *nameOfAccount = [account objectAtIndex:indexPath.row];
 cell.textLabel.text = nameOfAccount;
 NSString *accountNumber = [number objectAtIndex:indexPath.row];
 cell.detailTextLabel.text = accountNumber;

由于您要在同一单元格中显示数据,因此可以将名称和客户编号同时包含在字典或自定义模型对象中,以容纳这两个信息。

在您的plist中,这可能是字典对象的结构,数组

在此处输入图片说明

显示信息时。 为dataSource创建一个数组,说accounts

#define kAccountName @"Name"
#define kAccountNumber @"Number"

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSString *filePath = [[NSBundle mainBundle]pathForResource:@"Accounts" ofType:@"plist"];
    self.accounts = [NSArray arrayWithContentsOfFile:filePath];

}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [self.accounts count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    NSDictionary *account = self.accounts[indexPath.row];

    cell.textLabel.text = account[kAccountName];
    cell.detailTextLabel.text = account[kAccountNumber];

    // Configure the cell...

    return cell;
}

源代码

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM