问题从UITableView删除行

时间:2015-03-22 01:25:41

标签: ios objective-c uitableview

我正在使用Parse来填充一个工作正常的表视图。我试图从表格视图中删除一行,但收到​​此错误:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI length]: unrecognized selector sent to instance 0x7fd3f0f20f60'

以下是代码:

#import "CompletedJobs.h"
#import <Parse/Parse.h>

@interface CompletedJobs ()

@property (nonatomic, strong) NSMutableArray *jobs;
@property (nonatomic, strong) NSMutableArray *objectIds;
@property (nonatomic, strong) UIActivityIndicatorView *loadingIndicator;
@property (nonatomic, strong) UIRefreshControl *refresh;

@end

@implementation CompletedJobs

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.tableView setDataSource:self];
    [self.tableView setDelegate:self];

    self.navigationItem.rightBarButtonItem = self.editButtonItem;

    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:@"Completed"];
    [query setLimit:1000];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {

            for (NSDictionary *objectDictionary in objects) {
                [self.jobs addObject:objectDictionary];

                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)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;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSString *objectId = [self.objectIds objectAtIndex:indexPath.row];
        PFQuery *query = [PFQuery queryWithClassName:@"Completed"];
        [query getObjectInBackgroundWithId:objectId block:^(PFObject *job, NSError *error) {
            [job deleteInBackground];
        }];

        [self getJobs];
    }
}

- (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:UITableViewCellStyleSubtitle reuseIdentifier:@"UITableViewCell"];
    }

    if (cell) {
        cell.backgroundColor = [UIColor clearColor];
        cell.textLabel.textColor = [UIColor blackColor];

        cell.textLabel.text = [jobDictionary objectForKey:@"job"];

        NSString *completedBy = [jobDictionary objectForKey:@"completedBy"];

        NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
        formatter.dateStyle = NSDateFormatterLongStyle;
        formatter.timeStyle = NSDateFormatterShortStyle;
        NSString *completedDate = [formatter stringFromDate:[jobDictionary objectForKey:@"date"]];

        NSString *detailString = [NSString stringWithFormat:@"Completed by: %@ on %@", completedBy, completedDate];
        cell.detailTextLabel.text = detailString;
    }

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

}

@end

0 个答案:

没有答案