0

I am hoping someone can help me out with this issue. I have created a custom table view inside my view and I am downloading data from a sever and placing it into the table. Right now I have a custom cell and inside the custom cell is one label called "tracking". Everything seems fine but when I go to load my application it crashes and gives me the following error:

2016-01-10 23:34:06.107 uDropOff[88265:2009009] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key tableView.'

I hope someone can help, I will post the code for both the cell, and the view down below.

TrackCell.h

//
//  TrackCell.h
//  uDropOff 3
//
//  Created by Curtis Boylan on 10/01/2016.
//  Copyright © 2016 Curtis Boylan. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface TrackCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UILabel *tracking;


@end

TrackCell.m

//
//  TrackCell.m
//  uDropOff 3
//
//  Created by Curtis Boylan on 10/01/2016.
//  Copyright © 2016 Curtis Boylan. All rights reserved.
//

#import "TrackCell.h"

@implementation TrackCell

@synthesize tracking;


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

Track1.h

//
//  Track1.h
//  uDropOff 3
//
//  Created by Curtis Boylan on 06/01/2016.
//  Copyright © 2016 Curtis Boylan. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "TrackCell.h"

@interface Track1 : UIViewController <UITableViewDelegate,UITableViewDataSource>
{
    IBOutlet UITableView *tableData;
}
@property(strong,nonatomic)NSArray *arr;
@property (assign, nonatomic) IBOutlet TrackCell *customCell;
@property (weak, nonatomic) IBOutlet UITableView *table;

@end

Track1.m

//
//  Track1.m
//  uDropOff 3
//
//  Created by Curtis Boylan on 06/01/2016.
//  Copyright © 2016 Curtis Boylan. All rights reserved.
//

#import "Track1.h"
#import "TrackCell.h"

@interface Track1 ()

@end

@implementation Track1
{
    NSMutableArray *myObject;
    // A dictionary object
    NSDictionary *dictionary;
    // Define keys
    NSString *title;

}

- (void)viewDidLoad {
    [super viewDidLoad];
     [self webstuff1];
    // Do any additional setup after loading the view.
    UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
    [refreshControl addTarget:self action:@selector(handleRefresh:) forControlEvents:UIControlEventValueChanged];
    [self.table addSubview:refreshControl];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

-(void)handleRefresh:(UIRefreshControl *)refresh {

    // refresh.attributedTitle = [[NSAttributedString alloc] initWithString:@"Refreshing data..."];

    NSLog(@"refreshtest");
 //   [self webstuff1];
    [refresh endRefreshing];

}

- (void)webstuff1
{

    title = @"cust";
    myObject = [[NSMutableArray alloc] init];

    NSData *jsonSource = [NSData dataWithContentsOfURL:
                          [NSURL URLWithString:@"http://udropoff.com/login/driverlist.php"]];

    id jsonObjects = [NSJSONSerialization JSONObjectWithData:
                      jsonSource options:NSJSONReadingMutableContainers error:nil];

    for (NSDictionary *dataDict in jsonObjects) {
        NSString *title_data = [dataDict objectForKey:@"cust"];





        dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                      title_data, title,
                      nil];
        [myObject addObject:dictionary];
        [_table reloadData];
    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return myObject.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TrackCell * cell = [tableView dequeueReusableCellWithIdentifier:(@"TrackCell")];
    if (!cell)
    {
        [tableView registerNib:[UINib nibWithNibName:@"TrackCell" bundle:nil] forCellReuseIdentifier:@"TrackCell"];
        cell = [tableView dequeueReusableCellWithIdentifier:@"TrackCell"];
    }
    NSDictionary *tmpDict = [myObject objectAtIndex:indexPath.row];

    NSMutableString *text;


    text = [NSMutableString stringWithFormat:@"%@",
            [tmpDict objectForKeyedSubscript:title]];


    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    cell.backgroundColor = [UIColor clearColor];




    cell.tracking.text = text;



    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}
@end

Thanks!

Curtis Boylan
  • 827
  • 1
  • 7
  • 23
  • Where does the crash happen? You may need to add an exception breakpoint to see where the exception is thrown. – jcaron Jan 11 '16 at 00:25
  • Note that you should avoid using `NSData dataWithContentsOfURL:`, at the very least on the main thread. I recommend using `NSURLSession dataTaskWithURL:completionHandler:`, but at the very least you'll want to run this on a background thread. – jcaron Jan 11 '16 at 00:27
  • You also have weird names and types for your variations. `jsonObjects` should probably be defined as an `NSArray` (and you may want to check it's one). `myObject` is an array, that's a weird name for it. – jcaron Jan 11 '16 at 00:29
  • You definitely don't want to reload the table for every object in your JSON array... – jcaron Jan 11 '16 at 00:29
  • Why is `dictionary` an instance variable? It should be a local variable in your loop. I also recommend you use the modern Objective-C literals: `dictionary = @{ title: title_data}` – jcaron Jan 11 '16 at 00:30
  • You can also simplify your `text` assignment a lot: `text = tmpDict[title]` – jcaron Jan 11 '16 at 00:32
  • Hi all, I have done what you said and fixed the error of it crashing, now it loads but it is just an empty table. Anyone got anymore solutions? Thanks. – Curtis Boylan Jan 11 '16 at 09:05
  • Got it working, thanks all. Was simply I forgot to link the delegate and dataSource methods. – Curtis Boylan Jan 11 '16 at 09:45

0 Answers0