开发者

Unrecognized Selector when Using UITableView in a Subview

开发者 https://www.devze.com 2023-01-14 08:06 出处:网络
I am building an app that uses a TabBarController to display several other views. In one of these views, I\'m using a navigation controller to navigate some tabular data. When the user click\'s this t

I am building an app that uses a TabBarController to display several other views. In one of these views, I'm using a navigation controller to navigate some tabular data. When the user click's this tab, I load in the NavigationController which in turn loads the TableView that I'm using. The issue is that I get the following error upon load of the TableView Controller:

-[UIViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x6b37b10

I have read everywhere that this type of error usually comes from a misconnection in IB or that a class is not correct in the view controller. Here is my code and screenshots of IB as to help debug this for me.

Thanks in advance!

The Error I'm Receiving

Unrecognized Selector when Using UITableView in a Subview

The TableView's Connections in IB

Unrecognized Selector when Using UITableView in a Subview

The File Owner's Class

Unrecognized Selector when Using UITableView in a Subview

My Interface File

@interface FAQListViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> {

    // Dictionary that will hold the FAQ Key/Values
    NSMutableArray  *arrFAQData;

}

Implementation

@implementation FAQListViewController

// Implementation for UITableView numberOfRowsInSection protocol
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return [arrFAQData count];

}

// Implementation for UITableView cellForRowAtIndexPath protocol
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    // See if we have any cells available for reuse
    UITableViewCell *objCell = [tableView dequeueReusableCellWithIdentifier:@"FAQCell"];
    if (objCell == nil) {

        // No reusable cell exists, so let's create a new one
        objCell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: @"FAQCell"];

    }

    // Give it data
    NSDictionary *objRow = [arrFAQData objectAtIndex: [indexPath row]];
    objCell.textLabel.text = [objRow valueForKey:@"Title"];

    // Return the created cell
    return objCell;


}

// Selection Event Handler
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    // Get the value for the selected key
    NSDictionary *dictRow = [arrFAQData objectAtIndex:[indexPath row]];
    NSString *strURL = [dictRow valueForKey: @"URL"];

    // Alert result
    UIAlertView *objAlert;
    obj开发者_JAVA技巧Alert = [[UIAlertView alloc] initWithTitle:@"Alert" message: strURL delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; 

    // Release created objects
    [objAlert show];
    [objAlert release];

    // Deselect row
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}
- (void)viewDidLoad {

    // Pull in FAQ from Plist
    NSString *strFAQPlist = [[NSBundle mainBundle] pathForResource:@"FAQData" ofType:@"plist"];
    arrFAQData = [[NSMutableArray alloc] initWithContentsOfFile: strFAQPlist];


    [super viewDidLoad];
}

- (void)dealloc {

    [arrFAQData release];


    [super dealloc];
}


First of all, this is a well-constructed question, I wish everyone posted screen shots and full source code. :-)

It looks like the message is being sent to a generic UIViewController, instead of FAQListViewController. This tells me that perhaps when you instantiate the FAQListViewController, you are creating an instance of UIViewController instead?

To instantiate the FAQ list, you should be using something like:

[[FAQListViewController alloc] initWithNibName: @"FAQViewController" bundle: nil];

Your error might arise if you were instead instantiating it like:

[[UIViewController alloc] initWithNibName: @"FAQViewController" bundle: nil];


Try to make the Delegate & Datasource connections programmatically. I had a similar issue were an optional method of the UITableViewDelegate protocol was not being called for no apparent reason (while other delegate methods were working as expected) unless I made the connection programmatically. Since the method was optional, however, no exception was thrown.

0

精彩评论

暂无评论...
验证码 换一张
取 消