Jessica
Jessica

Reputation: 9830

Add object on a tableViewController

In the storyboard I have a static UItableViewController. I want to add a tableView on top of that tableVC. So I created a custom class that's a subclass of UITableViewController. Here's that code:

- (void)viewDidLoad {
    [super viewDidLoad];

    self.tableView.bounds = self.textField.bounds;
}

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

// -----------------------------------------------------------------------------
#pragma mark - TableView delegate
// -----------------------------------------------------------------------------
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.resultArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }

    [cell.textLabel setText:self.resultArray [indexPath.row]];

    return cell;
}

Then in the main viewControllers class I did the following:

resultTableVC *tableView = [[resultTableVC alloc] initWithStyle:UITableViewStylePlain];
tableView.textField = self.tagsTextField;

[self.view addSubview:tableView.tableView];

When I run the app, I get the following error message:

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<mainTableViewController 0x7fe35a550530> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key tagResultView.'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000102182c65 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x0000000101e1bbb7 objc_exception_throw + 45
    2   CoreFoundation                      0x00000001021828a9 -[NSException raise] + 9
    3   Foundation                          0x00000001019b1b53 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 259
    4   CoreFoundation                      0x00000001020cad50 -[NSArray makeObjectsPerformSelector:] + 224
    5   UIKit                               0x0000000102a9552b -[UINib instantiateWithOwner:options:] + 1506
    6   UIKit                               0x00000001028ed718 -[UIViewController _loadViewFromNibNamed:bundle:] + 242
    7   UIKit                               0x00000001028edd08 -[UIViewController loadView] + 109
    8   UIKit                               0x0000000102ab1df8 -[UITableViewController loadView] + 76
    9   UIKit                               0x00000001028edf79 -[UIViewController loadViewIfRequired] + 75
    10  UIKit                               0x00000001028ee40e -[UIViewController view] + 27
    11  UIKit                               0x0000000102ab1b73 -[UITableViewController tableView] + 30
    12  UIKit                               0x0000000102ab25ba -[UITableViewController _viewControllerWasSelected] + 22
    13  UIKit                               0x000000010292cf46 -[UITabBarController viewDidAppear:] + 140
    14  UIKit                               0x00000001028f1ff1 -[UIViewController _setViewAppearState:isAnimating:] + 567
    15  UIKit                               0x00000001028f2b3b -[UIViewController _executeAfterAppearanceBlock] + 52
    16  UIKit                               0x00000001027e162c _applyBlockToCFArrayCopiedToStack + 314
    17  UIKit                               0x00000001027e14c5 _afterCACommitHandler + 564
    18  CoreFoundation                      0x00000001020b5ca7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
    19  CoreFoundation                      0x00000001020b5c00 __CFRunLoopDoObservers + 368
    20  CoreFoundation                      0x00000001020aba33 __CFRunLoopRun + 1123
    21  CoreFoundation                      0x00000001020ab366 CFRunLoopRunSpecific + 470
    22  GraphicsServices                    0x0000000105fe8a3e GSEventRunModal + 161
    23  UIKit                               0x00000001027bd900 UIApplicationMain + 1282
    24  myBudget                            0x00000001013a852f main + 111
    25  libdyld.dylib                       0x0000000103bd9145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

I don't know what the error means, or how to fix it. (If this isn't correct) What's the proper way to add a tableView on top of an existing tableViewController?

Upvotes: 0

Views: 55

Answers (1)

Black Sheep
Black Sheep

Reputation: 1675

You are getting the error because there is no property called tagResultView. There is bad link to a mainTableViewController object that probably is specifying tagResultView. Check your storyboard/xib and remove any link to the undefined key specified(tagResultView). This happens when you remove something from the view controller improperly or when you rename views.

For creating an autocomplete UITextField I don't think you need to add a UITableView to another UITableView, Why not trying to have UIView or CustomTextField that will contain a UITextField and a UITableView for the autocomplete stuff then with the UITextFieldDelegate protocol you can present the UITableView anywhere you want it using Autolayout. Then just create a UITableViewCell and add the CustomTextField to it.

Upvotes: 1

Related Questions