PDA

View Full Version : Stuck on book example using Table View




KarlJay
Apr 15, 2011, 01:20 AM
I just bought the XCode 4 version of this:
http://www.techotopia.com/index.php/IPhone_iOS_4_Development_Essentials_Xcode_4_Edition

I'm on chapter 15 where I get several warnings and an error.

The warnings:
Incomplete implementation of class '*'
Incomplete implementation of class 'TableExampleViewController'

the error is on startup and says SIGABRT, If I disconnect the dataSource and delegate, the app will run, If I connect them per the book, it crashes.

The chapter is basically setting up a simple Table View, and using an array to load the data. I think they left something out of the book.

The '*' related warning seems to be a syntax error / typing error, but I can't seem to see it.

The SIGABRT seems to be related to the connection of the dataSource, which is an NSArray and set to File's Owner (as is delegate)

The website that sells the book also offers it for free online, I bought it last night and see no way to contact them or get source code.

On the up side, all the other examples are working fine.



dantastic
Apr 15, 2011, 02:41 AM
Post the code & the crash.

In the tableview delegate or data source methods, are you using any variables you haven't synthesized or anything like that?

jnoxx
Apr 15, 2011, 03:01 AM
Share some code.
The thing is, an incomplete implementation could mean alot of things, like methods u defined in your .H File, but didn't use yet in your .M File.
property's u made, but didnt synthesize yet (and don't forget to release!).
Post the code, and I'm sure we could help u out :)
Also the thing is, you probably don't have an UITableViewDelegate or an UITableViewController defined in your header file. That's why it's not recognizing the datasource/delegate, so it cannot call the methods which u may have defined in the .M File, so it gives a sigbart. All I can say is, show us teh code! :p

KarlJay
Apr 15, 2011, 03:54 AM
Most of the code is stock stuff that's generated from app template, the link in the 1st post has the entire chapter and code that they want you to add. I left out TableExampleAppDelegate.m as it's all stock, no code added there from the book example.

TableExampleAppDelegate.h


#import <UIKit/UIKit.h>

@class TableExampleViewController;

@interface TableExampleAppDelegate : NSObject <UIApplicationDelegate> {

}

@property (nonatomic, retain) IBOutlet UIWindow *window;

@property (nonatomic, retain) IBOutlet TableExampleViewController *viewController;

@end


TableExampleViewController.h


#import <UIKit/UIKit.h>

@interface TableExampleViewController : UIViewController
<UITableViewDelegate, UITableViewDataSource> // added from book example
{

NSArray *colorNames;
}

@property (nonatomic, retain) NSArray *colorNames;

@end


TableExampleViewControler.m

#import "TableExampleViewController.h"

@implementation TableExampleViewController // this gives warning incomplete implementation + method in protocol not implemented
@synthesize colorNames;

- (void)dealloc
{
[colorNames release];
[super dealloc];
}

//code from book example
// number of rows in the table view
- (NSInteger)tableview:(UITableView *)tableView
numberOfRowsInSection:(NSInteger) section{
return [self.colorNames count];
}

//code from book example
//change appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
//config the cell
cell.textLabel.text = [self.colorNames
objectAtIndex:[indexPath row]];
return cell;
}

- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
self.colorNames = [[NSArray alloc]
initWithObjects:@"Red", @"Green", @"Blue", @"Indigo", @"Violet", nil];

}

- (void)viewDidUnload
{
[super viewDidUnload];
self.colorNames = nil;
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end


Output:

2011-04-15 01:47:03.531 TableExample[12199:207] -[TableExampleViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x4e15ce0
2011-04-15 01:47:03.533 TableExample[12199:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[TableExampleViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x4e15ce0'
*** Call stack at first throw:
(
0 CoreFoundation 0x00dc15a9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x00f15313 objc_exception_throw + 44
2 CoreFoundation 0x00dc30bb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x00d32a04 ___forwarding___ + 1124
4 CoreFoundation 0x00d32522 _CF_forwarding_prep_0 + 50
5 UIKit 0x001d02b7 -[UISectionRowData refreshWithSection:tableView:tableViewRowData:] + 1834
6 UIKit 0x001cdd88 -[UITableViewRowData numberOfRows] + 108
7 UIKit 0x00081677 -[UITableView noteNumberOfRowsChanged] + 132
8 UIKit 0x0008e708 -[UITableView reloadData] + 773
9 UIKit 0x0008b844 -[UITableView layoutSubviews] + 42
10 QuartzCore 0x016aba5a -[CALayer layoutSublayers] + 181
11 QuartzCore 0x016adddc CALayerLayoutIfNeeded + 220
12 QuartzCore 0x016530b4 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 310
13 QuartzCore 0x01654294 _ZN2CA11Transaction6commitEv + 292
14 UIKit 0x000159c9 -[UIApplication _reportAppLaunchFinished] + 39
15 UIKit 0x00015e83 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 690
16 UIKit 0x00020617 -[UIApplication handleEvent:withNewEvent:] + 1533
17 UIKit 0x00018abf -[UIApplication sendEvent:] + 71
18 UIKit 0x0001df2e _UIApplicationHandleEvent + 7576
19 GraphicsServices 0x00ffa992 PurpleEventCallback + 1550
20 CoreFoundation 0x00da2944 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
21 CoreFoundation 0x00d02cf7 __CFRunLoopDoSource1 + 215
22 CoreFoundation 0x00cfff83 __CFRunLoopRun + 979
23 CoreFoundation 0x00cff840 CFRunLoopRunSpecific + 208
24 CoreFoundation 0x00cff761 CFRunLoopRunInMode + 97
25 UIKit 0x000157d2 -[UIApplication _run] + 623
26 UIKit 0x00021c93 UIApplicationMain + 1160
27 TableExample 0x00002519 main + 121
28 TableExample 0x00002495 start + 53
29 ??? 0x00000001 0x0 + 1
)
terminate called after throwing an instance of 'NSException'
Current language: auto; currently objective-c

//Main where it crashes:

#import <UIKit/UIKit.h>

int main(int argc, char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, nil); // point where it crashes
[pool release];
return retVal;
}

dantastic
Apr 15, 2011, 04:03 AM
So the answer is right there:

reason: '-[TableExampleViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x4e15ce0'

so that's the

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

so have a look inside that function, see what it does and what could be missing.
Hint: this piece is correct - you need to look for something that's missing elsewhere ;)

KarlJay
Apr 15, 2011, 05:34 AM
My guess right now is that the method is not defined in the @implementation so the program can't call a method that's not defined.

If that's the case, the book skipped over that part and I can't find an example of adding a method to the @implementation section.

Quote from the 'Dummies' book:
"@implementation (like @interface) is a compiler directive that says you’re about to present the code that implements a class. The name of the class appears after @implementation. Here is where you code the definitions of the individual methods."

So every method you add, you have to define in the @implementation, right?
It's probably a prototype method function starting with return type like in C++.

dantastic
Apr 15, 2011, 06:17 AM
Sorry,

I was actually wrong. I thought - incorrectly - that you'd missed another thing.

I see now that you've a typo in the declaration.

try this instead:

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

Sykte
Apr 15, 2011, 06:59 AM
The warnings:
Incomplete implementation of class '*'
Incomplete implementation of class 'TableExampleViewController'
.


This is telling you almost everything. You are not conforming to one of the protocols for UITableView. I would recommend reading the UITableView class reference http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableView_Class/Reference/Reference.html

and the TableView Programming Guide http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/TableView_iPhone/AboutTableViewsiPhone/AboutTableViewsiPhone.html%23//apple_ref/doc/uid/TP40007451

Shawnpk
Apr 15, 2011, 11:36 AM
You definitely have a typo in your numberOfRowsInSection method. Read it over carefully and compare it to what is in the book.

KarlJay
Apr 15, 2011, 03:09 PM
WOW! did cap the 1st V in View! Still getting used to the way the autocomplete works. If you press TAB it only takes some of the autocomplete.

OK thanks!

PhoneyDeveloper
Apr 15, 2011, 03:23 PM
Few comments,

It should have been obvious from the SIGABRT error message what the problem was. There are a few things that could cause the error but it specifically said that the selector didn't exist. Looking for the selector is the first thing to do. Case and spelling always count in C-based languages. Other causes of this error would be that the delegate/dataSource outlets are connected to the wrong object.

When I'm going to build a view controller that will act like a UITableViewController I always start with the UITableViewController that is built from the template file. It has a lot of the callback methods that you need. I change the base class to UIViewController and I'm ready to go.

In cases where I need a method I try to copy it from the header file. I'll admit that using the code completion is also convenient but you've now discovered one of its pitfalls.

One other thing, in the UITableViewDataSource protocol tableView:numberOfRowsInSection: is marked as required. This means that the compiler will warn you if you're missing that method. I guess you ignored that warning? I guess you won't ignore it again.

KarlJay
Apr 15, 2011, 07:31 PM
Your right, it did give a warning 1st, I was trying to figure out what it meant, but as I started this program, several warnings came up and went away (usually waiting for me to finish a needed method)

This is actually my 1st error! I just got things setup and started typing in examples from a book. Theres a lot of things to wrap the mind around, some are different than other languages (sometimes just the terms are different)

One thing that threw me off is the Google search for SIGABRT talks about memory problems and not releasing objects... and now I see that it does relate to this as the program is being told to call something that's not there.

As far as the code completion goes, it wouldn't allow me to type in cell, only Cell or (don't remember exactly) so I had to work around it. The keystrokes are different too, in MS tab selects the item, in Xcode it's enter. I Have to unlearn a few things...

By the way, thanks for everyone input!

dejo
Apr 16, 2011, 02:36 AM
One thing that threw me off is the Google search for SIGABRT talks about memory problems and not releasing objects...
SIGABRT is a run-time error. It's always wise to deal with your compile-time errors first. That is, Build your project. If you have errors/warnings, deal with them at this point. Don't try to run your app yet.. Then when they're gone, then you can Run your app. This is when run-time errors will pop up.