Ios 越界误差
我的应用程序有一个表视图,其中包含从解析表中获取的作业列表。用户可以点击导航栏上的“+”按钮转到另一个屏幕以创建新作业。创建新作业后,它将返回到作业列表,并将刚刚创建的作业添加到列表中。问题是,我可以选择列表中已经存在的两个作业,没有问题,但当我尝试选择新作业时,应用程序崩溃,出现以下错误:Ios 越界误差,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我的应用程序有一个表视图,其中包含从解析表中获取的作业列表。用户可以点击导航栏上的“+”按钮转到另一个屏幕以创建新作业。创建新作业后,它将返回到作业列表,并将刚刚创建的作业添加到列表中。问题是,我可以选择列表中已经存在的两个作业,没有问题,但当我尝试选择新作业时,应用程序崩溃,出现以下错误: Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 2 beyond bounds [0 .. 1]'
以下是作业列表的代码:
#import "UnassignedJobs.h"
#import "AppDelegate.h"
#import "NavController.h"
#import "LogInViewController.h"
#import "NewJobViewController.h"
#import "JobDetailViewController.h"
#import <Parse/Parse.h>
@interface UnassignedJobs ()
@property (nonatomic, strong) NSMutableArray *jobs;
@property (nonatomic, strong) NSMutableArray *objectIds;
@property (nonatomic, strong) UIActivityIndicatorView *loadingIndicator;
@property (nonatomic, strong) UIRefreshControl *refresh;
@end
@implementation UnassignedJobs
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView setDataSource:self];
[self.tableView setDelegate:self];
UIBarButtonItem *logoutButton = [[UIBarButtonItem alloc]initWithTitle:@"Logout" style:UIBarButtonItemStylePlain target:self action:@selector(logoutPressed)];
self.navigationItem.leftBarButtonItem = logoutButton;
UIBarButtonItem *newJobButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(createJob)];
self.navigationItem.rightBarButtonItem = newJobButton;
NSString *currentUserFullName = [[PFUser currentUser]objectForKey:@"Name"];
if ([currentUserFullName isEqualToString:@"Cory Pollard"] || [currentUserFullName isEqualToString:@"Richie Ray"]) {
newJobButton.enabled = YES;
}
else {
newJobButton.enabled = NO;
}
CGFloat width = CGRectGetWidth(self.view.bounds);
CGFloat height = CGRectGetHeight(self.view.bounds);
self.loadingIndicator = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(width / 2, height / 2, 37, 37)];
self.loadingIndicator.center = CGPointMake(width / 2, height / 2 - 37);
self.loadingIndicator.autoresizingMask = (UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin);
self.loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
self.loadingIndicator.hidesWhenStopped = YES;
[self.view addSubview:self.loadingIndicator];
[self.loadingIndicator startAnimating];
[self getJobs];
self.refresh = [[UIRefreshControl alloc]init];
self.refresh.tintColor = [UIColor blackColor];
[self.refresh addTarget:self action:@selector(refreshData) forControlEvents:UIControlEventValueChanged];
self.refreshControl = self.refresh;
}
- (void)viewDidAppear:(BOOL)animated {
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)getJobs {
self.jobs = [[NSMutableArray alloc]init];
self.objectIds = [[NSMutableArray alloc]init];
PFQuery *query = [PFQuery queryWithClassName:@"Jobs"];
[query setLimit:1000];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
for (NSDictionary *objectDictionary in objects) {
NSString *assigned = [objectDictionary objectForKey:@"assigned"];
if ([assigned isEqualToString:@"no"]) {
[self.jobs addObject:objectDictionary];
// self.objectIds = [objects valueForKeyPath:@"objectId"];
}
for (int i = 0; i < self.jobs.count; i++) {
[self.objectIds addObject:[self.jobs valueForKeyPath:@"objectId"]];
}
}
dispatch_async(dispatch_get_main_queue(), ^ {
[self.tableView reloadData];
[self.loadingIndicator stopAnimating];
});
}
else {
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}];
}
- (void)createJob {
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
NewJobViewController *jobCreateScreen = [[NewJobViewController alloc]initWithNibName:@"NewJobViewController" bundle:nil];
NavController *newJobNav = [[NavController alloc]initWithRootViewController:jobCreateScreen];
newJobNav.delegate = jobCreateScreen;
appDelegate.window.rootViewController = newJobNav;
}
- (void)logoutPressed {
[PFUser logOut];
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
LogInViewController *loginScreen = [[LogInViewController alloc]initWithNibName:@"LogInViewController" bundle:nil];
NavController *loginNavController = [[NavController alloc]initWithRootViewController:loginScreen];
loginNavController.delegate = loginScreen;
appDelegate.window.rootViewController = loginNavController;
}
- (void)refreshData {
[self getJobs];
[self.refresh endRefreshing];
}
#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.jobs.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
NSDictionary *jobDictionary = [self.jobs objectAtIndex:[indexPath row]];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
}
if (cell) {
cell.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor blackColor];
cell.textLabel.text = [jobDictionary objectForKey:@"job"];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *jobDictionary = [self.jobs objectAtIndex:[indexPath row]];
JobDetailViewController *jobDetails = [[JobDetailViewController alloc]initWithNibName:@"JobDetailViewController" bundle:nil];
jobDetails.jobName = [jobDictionary objectForKey:@"job"];
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
// [formatter setDateFormat:@"MM-dd-yyyy"];
formatter.dateStyle = NSDateFormatterLongStyle;
formatter.timeStyle = NSDateFormatterShortStyle;
jobDetails.jobDate = [formatter stringFromDate:[jobDictionary objectForKey:@"date"]];
jobDetails.objectId = [[self.objectIds objectAtIndex:indexPath.row]objectAtIndex:indexPath.row];
jobDetails.assignedWorker = [jobDictionary objectForKey:@"worker"];
jobDetails.details = [jobDictionary objectForKey:@"details"];
[self.navigationController pushViewController:jobDetails animated:YES];
}
@end
#导入“UnassignedJobs.h”
#导入“AppDelegate.h”
#导入“NavController.h”
#导入“LogInViewController.h”
#导入“NewJobViewController.h”
#导入“JobDetailViewController.h”
#进口
@接口未分配作业()
@属性(非原子,强)NSMutableArray*作业;
@属性(非原子,强)NSMutableArray*ObjectId;
@属性(非原子,强)UIActivityIndicatorView*加载指示器;
@属性(非原子,强)UIRefreshControl*刷新;
@结束
@未分配作业的实现
-(无效)viewDidLoad{
[超级视图下载];
[self.tableView setDataSource:self];
[self.tableView setDelegate:self];
UIBarButtonItem*logoutButton=[[UIBarButtonItem alloc]initWithTitle:@“注销”样式:UIBarButtonItemStylePlain目标:自我操作:@selector(logoutPressed)];
self.navigationItem.leftBarButtonItem=注销按钮;
UIBarButtonItem*newJobButton=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAddTarget:self action:@selector(createJob)];
self.navigationItem.rightBarButtonItem=newJobButton;
NSString*currentUserFullName=[[PFUser currentUser]objectForKey:@“Name”];
if([currentUserFullName IseQualtString:@“Cory Pollard”]| |[currentUserFullName IseQualtString:@“Richie Ray”])){
newJobButton.enabled=是;
}
否则{
newJobButton.enabled=否;
}
CGFloat width=CGRectGetWidth(self.view.bounds);
CGFloat height=CGRectGetHeight(self.view.bounds);
self.loadingIndicator=[[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(宽度/2,高度/2,37,37)];
self.loadingIndicator.center=CGPointMake(宽/2,高/2-37);
self.loadingIndicator.autoresizingMask=(UIViewAutoresizingFlexibleRightMargin | uiviewautoresizingflexibleleleftmargin | uiviewautoresizingflexiblebtotommargin | uiviewautoresizingflexibleptopmargin | uiviewautoresizingflexibleptommargin;
self.loadingIndicator.activityIndicatorViewStyle=UIActivityIndicatorViewStyleGray;
self.loadingIndicator.hidesWhenStopped=是;
[self.view addSubview:self.loadingIndicator];
[自加载指示器启动激活];
[自谋职业];
self.refresh=[[UIRefreshControl alloc]init];
self.refresh.tintColor=[UIColor blackColor];
[self.refresh addTarget:self action:@selector(refreshData)forControlEvents:UIControlEventValueChanged];
self.refreshControl=self.refresh;
}
-(无效)视图显示:(BOOL)动画{
}
-(无效)未收到记忆警告{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
-(无效)获得工作{
self.jobs=[[NSMutableArray alloc]init];
self.objectIds=[[NSMutableArray alloc]init];
PFQuery*query=[PFQuery queryWithClassName:@“作业”];
[查询设置限制:1000];
[查询findObjectsInBackgroundWithBlock:^(NSArray*对象,NSError*错误){
如果(!错误){
for(对象中的NSDictionary*objectDictionary){
NSString*assigned=[objectDictionary objectForKey:@“assigned”];
如果([分配的IsequalString:@“否”]){
[self.jobs addObject:objectDictionary];
//self.objectId=[objects valueForKeyPath:@“objectId”];
}
for(int i=0;i#import "NewJobViewController.h"
#import "AppDelegate.h"
#import "NavController.h"
#import "TabController.h"
#import "UnassignedJobs.h"
#import "AssignedJobs.h"
#import "MyJobs.h"
#import "Users.h"
#import "CompletedJobs.h"
#import <Parse/Parse.h>
@interface NewJobViewController ()
@property (weak, nonatomic) IBOutlet UITextField *jobName;
@property (weak, nonatomic) IBOutlet UITextView *detailTextView;
@property (weak, nonatomic) IBOutlet UIDatePicker *datePicker;
@property (weak, nonatomic) IBOutlet UIButton *createJobButton;
@property (nonatomic, strong) NSDate *jobDate;
@property (nonatomic, strong) NSString *dateString;
@end
@implementation NewJobViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.title = @"Create Job";
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain target:self action:@selector(cancel)];
self.navigationItem.leftBarButtonItem = cancelButton;
[self.datePicker addTarget:self action:@selector(updateDateString) forControlEvents:UIControlEventValueChanged];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)returnToMainScreen {
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
UnassignedJobs *unassignedJobs = [[UnassignedJobs alloc]initWithNibName:@"UnassignedJobs" bundle:nil];
unassignedJobs.title = @"Unassigned";
NavController *navController = [[NavController alloc]initWithRootViewController:unassignedJobs];
navController.delegate = unassignedJobs;
AssignedJobs *assignedJobs = [[AssignedJobs alloc]initWithNibName:@"AssignedJobs" bundle:nil];
assignedJobs.title = @"Assigned";
NavController *assignedNav = [[NavController alloc]initWithRootViewController:assignedJobs];
assignedNav.delegate = assignedJobs;
CompletedJobs *completed = [[CompletedJobs alloc]initWithNibName:@"CompletedJobs" bundle:nil];
completed.title = @"Completed";
NavController *completedNav = [[NavController alloc]initWithRootViewController:completed];
completedNav.delegate = completed;
MyJobs *myJobs = [[MyJobs alloc]initWithNibName:@"MyJobs" bundle:nil];
myJobs.title = @"My Jobs";
NavController *myNav = [[NavController alloc]initWithRootViewController:myJobs];
myNav.delegate = myJobs;
Users *userList = [[Users alloc]initWithNibName:@"Users" bundle:nil];
userList.title = @"Users";
NavController *userNav = [[NavController alloc]initWithRootViewController:userList];
userNav.delegate = userList;
TabController *tabController = [[TabController alloc]init];
tabController.viewControllers = @[navController, assignedNav, completedNav, myNav, userNav];
appDelegate.window.rootViewController = tabController;
}
- (void)updateDateString {
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
formatter.dateStyle = NSDateFormatterLongStyle;
formatter.timeStyle = NSDateFormatterMediumStyle;
self.dateString = [formatter stringFromDate:self.datePicker.date];
self.jobDate = [formatter dateFromString:self.dateString];
}
- (IBAction)createJob:(id)sender {
PFObject *job = [PFObject objectWithClassName:@"Jobs"];
job[@"job"] = self.jobName.text;
job[@"details"] = self.detailTextView.text;
job[@"assigned"] = @"no";
job[@"date"] = self.jobDate;
[job saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (succeeded) {
// Close this window and return to unassigned jobs
[self returnToMainScreen];
}
else {
NSString *errorString = [[error userInfo] objectForKey:@"error"];
UIAlertView *errorAlert = [[UIAlertView alloc]initWithTitle:@"Job could not be saved!" message:errorString delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[errorAlert show];
}
}];
}
- (void)cancel {
[self returnToMainScreen];
}
# pragma mark UITextView Delegate Methods
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if([text isEqualToString:@"\n"]) {
[textView resignFirstResponder];
return NO;
}
return YES;
}
# pragma mark UITextField Delegate Methods
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return NO;
}
@end
@implementation UnassignedJobs ...
- (void)viewDidAppear:(BOOL)animated {
[self getJobs];
[self.table reloadData];
}
- (void)getJobs {
[self.jobs removeAllObjects];
[self.objectIds removeAllObjects];
self.jobs = [[NSMutableArray alloc]init];
self.objectIds = [[NSMutableArray alloc]init];
...}