いくつかの静的行を含む静的セル UITableViewController があります。コントローラー自体には 3 つのセクションがあります。ユーザーがモーダルセグエを介して他のコントローラーから生成された新しいオブジェクトを追加するたびに、行を追加するつもりでした。これらすべてをストーリーボードを使用して実装し、作成したカスタム UITableViewController に接続しました。
まず、ストーリーボードに tableView コントローラーがあります。次に、以下のメソッドをオーバーライドする UITableViewController のカスタム クラスを作成します。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
if (section == 2) {
return [appDelegate.userIds count];
} else {
return [super tableView:tableView numberOfRowsInSection:section];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 2) {
UITableViewCell *cell;
cell = [tableView dequeueReusableCellWithIdentifier:@"userIdCell"];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"userIdCell"];
}
appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
NSLog([NSString stringWithFormat:@"%d", [appDelegate.userIds count]]);
NSLog([[appDelegate.userIds objectAtIndex:indexPath.row] userId]);
cell.textLabel.text = [[appDelegate.userIds objectAtIndex:indexPath.row] userId];
return cell;
} else if (indexPath.section == 1) {
UITableViewCell *cell;
cell = [tableView dequeueReusableCellWithIdentifier:@"addCell"];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"addCell"];
}
if (indexPath.row == 0) {
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text = @"Add User ID";
}
return cell;
} else {
return [super tableView:tableView cellForRowAtIndexPath:indexPath];
}
}
appDelegate.userIds は、AppDelegate クラスからインスタンス化された可変配列であり、appDelegate はそのインスタンスであることに注意してください。
他の入力フォーム viewController へのモーダル セグエ経由で、ユーザー オブジェクトを appDelegate.userIds 配列に追加します。
これがセグエの準備です
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"tambahUser"])
{
UINavigationController *navigationController = segue.destinationViewController;
TambahUserViewController *TambahUserViewController = [[navigationController viewControllers] objectAtIndex:0];
TambahUserViewController.delegate = self;
}
}
これは ControllerDelegate から実装したメソッドです
#pragma mark - tambahUserViewControllerDelegate
- (void)tambahUserViewController:(TambahUserViewController *)controller didAddUser:(User *)user
{
[appDelegate.userIds addObject:user];
NSInteger lastRowIndex = [appDelegate.userIds count] - 1;
NSIndexPath* indexPath = [NSIndexPath indexPathForRow:lastRowIndex inSection:2];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)tambahUserViewControllerDidCancel:(TambahUserViewController *)controller
{
[self dismissViewControllerAnimated:YES completion:nil];
}
最初、3 番目のセクション (セクション 2) には行がないため、最初の行を追加しました。最初のユーザーを追加した後、問題はありませんでした。2行目を追加した後にエラーが表示されました。と言いました、
> 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
私のコードの何が問題なのか教えてください。ありがとうございました。