更新我的表视图时,我的应用程序崩溃了

时间:2014-07-21 04:02:02

标签: ios uitableview

我正在做一个基于数据库的应用程序。我的应用程序需要更新数据库表,同时需要更新表视图,即根据特定的数据库表生成。

这里是我写的代码

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.courses.count;
}

-(void)viewWillAppear:(BOOL)animated {

    self.usernameLBL.text = [NSString stringWithFormat:@"Welcome, %@", self.del.username];

    [self loadCourses];

//    NSLog(@"The courses count is %d", self.courses.count);

}

-(void)loadCourses {

    NSString * stringUrl = [NSString stringWithFormat:"Some URL";

    NSURL * uRL = [NSURL URLWithString:[stringUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    NSURLRequest * request = [NSURLRequest requestWithURL:uRL];

    NSError * error;

    NSData * results = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];

    self.courses = [NSJSONSerialization JSONObjectWithData:results options:0 error:&error];

    NSLog(@"The courses array count is %d", self.courses.count);

    [self.tableView reloadData];
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    NSDictionary * dict = self.courses[indexPath.row];

    cell.textLabel.text = dict[@"name"];

    cell.detailTextLabel.text = dict[@"id"];

    return cell;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.courses = [[NSMutableArray alloc] init];

    self.del = [[UIApplication sharedApplication] delegate];

    NSString * strURL = [NSString stringWithFormat:"Some URL";

//    NSLog(@"The username is %@", self.del.username);

    NSURL * url = [NSURL URLWithString:[strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    NSURLRequest * request = [NSURLRequest requestWithURL:url];

    NSError * error;

    NSData * data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];

    //    NSLog(@"The data is %@", data);

    NSDictionary * result = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];

    //    NSLog(@"The error is %@", error);

//    NSLog(@"The value returned is %@", result[@"image"]);

    if(![result[@"image"] isEqualToString:@"empty"]){

        NSString * urlLocation = result[@"image"];

        self.adminIMG.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[urlLocation stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]]]];
    }

    // Do any additional setup after loading the view.
}

- (IBAction)addNewCourse:(id)sender {

    NSString * strURL = [NSString stringWithFormat:"Some URL";

    NSURL * url = [NSURL URLWithString:[strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    NSURLRequest * request = [NSURLRequest requestWithURL:url];

    NSError * error;

    NSData * results = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];

    NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:results options:0 error:&error];

    if([dict[@"response"] isEqualToString:@"success"]) {

        NSLog(@"New course added");

        UIAlertView * success = [[UIAlertView alloc] initWithTitle:@"New course added successfully" message:@"You successfully added a new course" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];

        [success show];

    }else {

        NSLog(@"Course not added");
    }

    [self.courses removeAllObjects];

    [self viewDidLoad];

    [self viewWillAppear:YES];
}

我收到了这个错误。

  

错误:评估[13335:60b] * 由于未捕获的异常终止应用' NSInvalidArgumentException',原因:' - [__ NSCFArray objectForKeyedSubscript:]:无法识别的选择器已发送例如0xb7d26d0'

我在互联网上尝试了很多解决方案,但那些对我不起作用。请帮助我的任何人。在此先感谢:)

3 个答案:

答案 0 :(得分:1)

您的问题可能就在这里

[self.courses removeAllObjects];
[self viewDidLoad];
[self viewWillAppear:YES];

您不必清除阵列,请尝试以下操作:

<强>功能

- (void)addCourse{

    NSString * strURL = [NSString stringWithFormat:"Some URL";    
    NSURL * url = [NSURL URLWithString:
       [strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];    
    NSURLRequest * request = [NSURLRequest requestWithURL:url];    
    NSError * error; 
    NSData * results = [NSURLConnection sendSynchronousRequest:request
                                             returningResponse:nil error:&error];
    NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:results 
                                                          options:0 error:&error];

    if([dict[@"response"] isEqualToString:@"success"]) {    
        NSLog(@"New course added");    
        UIAlertView * success = [[UIAlertView alloc] 
                                 initWithTitle:@"New course added successfully" 
                                  message:@"You successfully added a new course" 
                                      delegate:nil
                             cancelButtonTitle:@"OK" 
                             otherButtonTitles: nil];    
        [success show];    
    }else {
            NSLog(@"Course not added");
    }
}

你的IBAction保持简单,你不想从IBAction发起生命周期事件,而是调用你不需要提及的方法来调用{{1在} viewDidLoad方法之前......

viewWillAppear

答案 1 :(得分:0)

首先检查它有效的array数据。(放置断点并检查) 我认为你有错误,因为你仍然没有将整数转换为字符串。 我将此行替换为tableview cellForRowAtIndexPath

cell.detailTextLabel.text = dict[@"id"];

替换

cell.detailTextLabel.text =[NSString stringWithFormat:@"%d",dict[@"id"]];

它可能对你非常有帮助谢谢。

答案 2 :(得分:0)

我认为您的问题就在这里..您的定位AppDelegateself.delegate

self.del = [[UIApplication sharedApplication] delegate];

AppDelegate仅在应用程序启动时运行一次,这就是为什么会出现错误