0

プロジェクトから plist を読み込もうとしていますが、誤って plist を削除するまでは機能していました。plist には、それぞれ 2 つの要素を持つ 5 つの配列があります。プログラムが配列の範囲を超えてアクセスしようとしていることは知っていますが、このインデックスがどこに設定されているかはわかりません。攻撃対象のコードは次のとおりです。このコードは 2 回正常に実行された後、何らかの理由で 3 回目にアクセスしようとし、最初の行で実行されます。なぜでしょうか?

次の例外がスローされます。

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;
4

1 に答える 1

1

同じセルにデータを表示しているため、アカウントの名前と番号の両方をディクショナリまたは両方の情報を保持するカスタム モデル オブジェクトに含めることができます。

あなたの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;
}

ソースコード

于 2013-06-09T06:32:22.417 に答える