UITableView error - iphone

ViewController.m
#pragma mark -
#pragma mark Table view data source
// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 0;
}
// Customize the 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];
}
// Configure the cell.
return cell;
}
ViewController.h
#interface PSLEViewController : UIViewController <UITableViewDelegate,UITableViewDataSource>
{
IBOutlet UITableView *highScoreTable;
}
#property(retain,nonatomic) UITableView *highScoreTable;
Error:
2010-12-04 02:20:15.541 PSLE[14369:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[PSLEViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x3b13080'
2010-12-04 02:20:15.542 PSLE[14369:207] Stack: (

Theres nothing wrong with that code. There may be some other underlying issues, maybe in a XIB. Are you sure the highScoreTable is connected?

Can you check the dataSource and delegate of UITableView in -(void)viewDidLoad?
They should not be nil after loaded from nib.

hai FYI are you allocate the tableView properly? check it out please
example highScoreTable = [[UITableView alloc]init];

Related

Programmatically filled TableView error

I get this error when I scroll my table view down:
2012-04-23 09:32:36.763 RedFox[30540:207] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 12 beyond bounds [0 .. 11]'
*** First throw call stack:
(0x13c3052 0x1554d0a 0x13af674 0x5794 0xb3e0f 0xb4589 0x9fdfd 0xae851 0x59322 0x13c4e72 0x1d6d92d 0x1d77827 0x1cfdfa7 0x1cffea6 0x1cff580 0x13979ce 0x132e670 0x12fa4f6 0x12f9db4 0x12f9ccb 0x12ac879 0x12ac93e 0x1aa9b 0x2158 0x20b5)
terminate called throwing an exceptionsharedlibrary apply-load-rules all
Current language: auto; currently objective-c
An in my .h file I have:
#interface MyTableView : UIViewController <UITableViewDataSource> {
int currentRow;
}
#property (strong,nonatomic) UITableView *tableView;
#property (strong,nonatomic) ViewBuilder *screenDefBuild;
In my .m file:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [screenDefBuild.elementsToTableView count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
}
ScreenListElements *currentScreenElement = [screenDefBuild.elementsToTableView objectAtIndex:currentRow]; //exception points here!
cell.textLabel.text = currentScreenElement.objectName;
currentRow++;
return cell;
}
- (void)viewDidLoad
{
[super viewDidLoad];
tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
[tableView setDataSource:self];
[self.view addSubview:tableView];
}
What's wrong with that?
The currentRow variable is unnecessary and causing issues!
To fix:
change the line
ScreenListElements *currentScreenElement = [screenDefBuild.elementsToTableView objectAtIndex:currentRow]; //exception points here!
to
ScreenListElements *currentScreenElement = [screenDefBuild.elementsToTableView objectAtIndex:indexPath.row]; //exception points here!
Reason:
you increment the currentRow each time cellForARowAtIndexPath: is called, which is wrong, as this method in not only called when displaying following cells(when scrolling down) but also when you scroll up(so currentRow should decrement then). That's why Apple put the parameter indexPath so you can easyily determine which cell tableView is requesting.
The currentRow doesn't really make sense to me.
To return a cell containing a row of your source array (elementsToTableView)
you need to ask for the row in the current index path.
Your error-causing line should look like this:
ScreenListElements *currentScreenElement = [screenDefBuild.elementsToTableView objectAtIndex: indexPath.row];
And you don't need currentRow at all.
Why did you implement it that way?

iPad:Popover sample to iOS5

I am developing an app which will be used on both iPhone and iPad.
Today I've found that I should use Popover for iPad instead of PickerView on an action sheet.
I am trying to use a sample app use it on iOS5, but I have been getting an error;
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason:
'-[UITableViewController loadView] loaded the "2-view-5" nib but didn't get a UITableView.'
*** First throw call stack:
Do you know what exactly i need to do to fix that?
And, do you have any recommendation of popover examples for iOS5?
UYLMasterViewController.m
#import "UYLMasterViewController.h"
#import "UYLDetailViewController.h"
#implementation UYLMasterViewController
#synthesize detailViewController = _detailViewController;
#pragma mark -
#pragma mark === View Management ===
#pragma mark -
- (void)viewDidUnload
{
[super viewDidUnload];
self.detailViewController = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
#pragma mark -
#pragma mark === Table View Delegates ===
#pragma mark -
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 20;
}
- (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];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [NSString stringWithFormat:#"Item %u", row+1];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger item = [indexPath row] +1;
NSNumber *detailItem = [NSNumber numberWithInteger:item];
if (self.detailViewController)
{
self.detailViewController.detailItem = detailItem;
}
}
#end
If you didn't modify anything from the sample app, this is quite strange.
Anyway, I usually get this error when I forgot to set an IBOutlet connection from the table to the controller UITableViewController.
It is likely that you delete it by mistake, or Xcode lost its connection for some reason (it happens).
See if UYLMasterViewController.h is a UITableViewController. If yes, then the UITableView must be the first view in nib file, directly connected to the 'view' IBOutlet property of the controller.
Otherwise if UITableView is inside a UIView then you get the exact error you mentioned.
A probable solution, if UITableView is not the main view, would be to set UYLMasterViewController as a normal UIViewController and give it all the delegate accessor, then drag a connection from the UITableView to the controller, you will see that you have two options delegate and datasource, you need both.

App Crashes due to UITableViewScrollPositionMiddle when testing on phone but not emulator

I have a uitableview with an array of dictionary values producing a fairly big list.
That the user can select from.. once the user selects a cell I pop the view from the navigational stack and pass all the values back to the main view.. I then allow the user to go back to this view if they have made a mistake and would like to change their input.
when they click back to that view I have a method that automatically scrolls to the old selected view using UITableViewScrollPositionMiddle.
However I think its causing some errors when being used on the phone, as the emulator works fine.
here is where I think the error is happening
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
//Center previously selected cell to center of the screen
[self.tableView scrollToRowAtIndexPath:oldCheckedData atScrollPosition:UITableViewScrollPositionMiddle animated:YES]; //<--- this method
}
and this is the error I get in the log when building it to my phone. The process in order to get this error is for the user to click on the cell in the parent view that loads the subview, the user selects a cell in the subview and is taken back to the parent view.. then when the user goes back to the same subview this shows up in the log.
2011-10-19 15:00:05.790 icode[1564:707] -[__NSArrayM section]: unrecognized selector sent to instance 0x181cd0
2011-10-19 15:00:05.797 icode[1564:707] CoreAnimation: ignoring exception: -[__NSArrayM section]: unrecognized selector sent to instance 0x181cd0
(there is no scroll effect)
then the user selects a different cell and this app crashes and this shows up in the log
2011-10-19 15:01:08.272 icode[1564:707] -[__NSArrayM row]: unrecognized selector sent to instance 0x181cd0
2011-10-19 15:01:08.275 icode[1564:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM row]: unrecognized selector sent to instance 0x181cd0'
*** First throw call stack:
(0x35e9e8b3 0x366e61e5 0x35ea1acb 0x35ea0939 0x35dfb680 0x334a76cf 0x3353c713 0x30e1d 0x3352cd69 0x335a60ab 0x35cc32ab 0x35e72a57 0x35e726bd 0x35e71293 0x35df44e5 0x35df43ad 0x30fa4fed 0x334a7dc7 0x24f7 0x24a0)
terminate called throwing an exception(gdb)
any help would be greatly appreciated.
Tableview data sources
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return [arraysByLetter count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSString *currentLetter = [sectionLetters objectAtIndex:section];
return [[arraysByLetter objectForKey:currentLetter] count];
}
/*
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
//override state-based properties set earlier by the table view, such as selection and background colorset up shit here
}
*/
- (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];
}
//Customize cell here
cell.selectionStyle = UITableViewCellSelectionStyleNone; // no blue selection
//Replaces previously selected cells accessory view (tick)
if (indexPath == oldCheckedData)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
//Display cells with data
NSArray *keys = [self.arraysByLetter objectForKey:[self.sectionLetters objectAtIndex:indexPath.section]];
NSString *key = [keys objectAtIndex:indexPath.row];
cell.textLabel.text = key;
return cell;
}
//Section headers
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [sectionLetters objectAtIndex:section];
}
//Section header titles
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return sectionLetters;
}
It looks like the index path you have a reference to at oldCheckedData may have been deallocated when the view controller was popped from the stack. Make sure you retain it before the view controller goes away.

custom uitableViewCell in controller which inherit from UITableViewController

I want to create custom UiTableViewCell, and it works, when I create TableView, like in this example, by inherit from UIViewController. But when I create controller which inherit from UITableViewController (not UIViewController), and in xib create UITableViewCell, and of course hook up IBOutlet, i get this error:
*** Assertion failure in -[UITableView _createPreparedCellForGlobalRow:withIndexPath:], /SourceCache/UIKit_Sim/UIKit-1448.89/UITableView.m:5678
2011-05-06 07:21:34.107 tabela[11424:207] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { return cellOne; }
This is the sample reference
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// In your case it has to be the custom cell object here.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"AnIdentifierString"];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:#"AnIdentifierString"] autorelease];
}
cell.textLabel.text = #"This text will appear in the cell";
return cell;
}

iPhone: UITableView, Dragging Table Contents Causes Crash

I'm new to iPhone application development. I'm trying to understand how to use UITableView.
I'm wrote simple code:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1 ;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
}
cell.textLabel.text = #"Hello";
return cell;
}
UITable shows content, but if i'm drag table contents my application terminates. You can see video: http://www.youtube.com/watch?v=TucTVJVhSD0
I tried to everything with array:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1 ;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [hello count] ;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
}
cell.textLabel.text = [hello objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
NSLog(#"Selected") ;
}
- (void) awakeFromNib
{
hello = [[NSArray alloc] initWithObjects:#"hello", #"world", #"end", nil];
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
Content is shown and if I'm selecting item I'm getting:
[Session started at 2010-03-16 19:21:48 +0200.]
2010-03-16 19:21:52.295 ViewTest[1775:207] *** -[ViewTestViewController respondsToSelector:]: message sent to deallocated instance 0x3911ec0
I'm total new to iPhone programming. And as I see everything I do - I'm just getting application terminated ..
Your table view code looks fine. Did you implement any other delegate methods in the ViewTestViewController?
Try running the application in the debugger. When it crashes, look at the stack trace. It should give a better hint.
(1) I think you may have released your tableview controller instance and killed it while it was still in use so that when the tableview sends it one of its delegate or datasource messages it crashes.
(2) Alternatively, the error message says that you are attempting to send the respondsToSelector: message to an instance of ViewTestViewController when you should be sending it to the class. (-[ViewTestViewController respondsToSelector:] vs + [ViewTestViewController respondsToSelector:] the "-" and "+" make all the difference.)
So, somewhere in your code, probably your app delegate, you've got code that says:
ViewTestViewController *myTableViewController= //..however you setup the controller
[myTableViewController respondsToSelector:#selector(someMethod)];
...when you should have...
[ViewTestViewController respondsToSelector:#selector(someMethod)];
I think (1) the most likely.
Thanks to all. I found problem. I found i haven't connected viewController IBOutlet with App Delegate. But why View was shown ?
#interface ViewTestAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
ViewTestViewController *viewController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet ViewTestViewController *viewController;