Adding a Navigation to a View Controller with a TableView - ios

I'm trying to add (or have) a navigation bar (or items) on the top of a View controller and and a tableview right below the navigation bar. The problem I'm having is when I set the tableview's datasource to View Controller, my app will crash (setting the tableview's delegate to View controller doesn't). For now, the code for the tableview are in the default ViewController.m. Do I have the tableview's code in the right place or did I connect something improperly?
Here's the error that it gives me:
2013-10-10 15:14:48.442 SomeApp[15058:a0b] -[UIViewController >tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x8d42450
2013-10-10 15:14:48.450 SomeApp[15058:a0b] *** Terminating app due to uncaught exception >'NSInvalidArgumentException', reason: '-[UIViewController >tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x8d42450'

Make sure your view controller adheres to the proper protocols:
CustomViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
And add the necessary methods from these protocols to your implementation. These are the documentation links for your ease:
UITableViewDelegate Protocol Reference
UITableViewDataSource Protocol Reference

You are missing some methods that are required for your tableview to work,
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
More information on UITableViewDataSource Protocol Reference

Related

Terminating app due to uncaught exception 'NSRangeException', reason:-[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'

I have been trying to figure out why this error is happening for a while
I put the tableview in view controller.
Set the tableview delegate and datasource to view controller.
I have implemented the delegate and datasource protocols.
I have done the same scene in fresh project almost 4 times, It worked fine. When I am trying to implement this scene in my current projects I see this error.
If I remove setting the delegate of table view then everything goes fine.
It is happening after returning cell.
What could be the reason? I am unable to figure it.I am using no arrays too.
Try adding an all-exception breakpoint (Add Exception Breakpoint) to debug where the exception occures. Maybe it is not even in that viewcontroller.
1) Use Exception Break points...As per the above answer
make sure if the delegate methods were set in previous scenes of the project.
I used all the below methods:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath;
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;

UITableView crashing when scrolled (iOS 7.1.2)

I create a UITableView when a user presses a button, set it's delegate and data source to the current controller (which implements both UITableViewDelegate and UITableViewDataSource protocols), add it as a subview to the controller's view and implement the data source methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
Whenever I scroll the table view, the application crashes with
[UITableView currentPage]: unrecognized selector sent to instance 0x11365c00
where 0x11365c00 is the tableView. If I don't set the tableView's delegate and only set the data source, it doesn't crash. This behavior is the same if I return 0 in numberOfRowsInSection, so that nothing is added to the tableView and there is no crash related to my data. Any help would be great!
The crash occurs because you are sending a message -currentPage to an object that can't handle it.
And actually tableviews doesn't have this method in their interfaces, unless you are using a category. So the question is which object n your implementation file should handle this message?
Reason:
[someObjectHere currentPage]; // Here someObjectHere is really a table view(UITableView type)
If you have table view class and forgot to define the currentPage method, then define it there. That's y it crashed the app.
If you have currentPage method in your view controller then you have to make sure that someObjectHere is a your View controller.
[self currentPage];

unrecognized selector sent to instance on ios table view cell click

I'm a very beginner to iOs. I'm going to view a list in table view controller. My application is a story board application. I did the following code to go to the next view.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
LawyerViewController *dvc = [self.storyboard instantiateViewControllerWithIdentifier:#"LawyerViewController"];
dvc.lawyer = #"Thisara";//[self.myObject objectAtIndex:indexPath.row];
[self.navigationController pushViewController:dvc animated:YES];
}
in my LawyerViewController header file I defined the following property.
#property (nonatomic, strong) NSString *lawyer;
When I'm clicking on the cell, I've got following error.
2014-07-26 23:56:14.420 lawyerapp[3626:60b] -[UIViewController setLawyer:]: unrecognized selector sent to instance 0x8cdd1f0
2014-07-26 23:56:14.424 lawyerapp[3626:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewController setLawyer:]:
unrecognized selector sent to instance 0x8cdd1f0'
How do I fix this.
Thanks in Advance!
You have not defined the class in storyBoard.
Go to your viewController in StoryBoard ->
than identity Inspector -> class -> Put the class LawyerViewController.
Clean,build and run

UICollectionView - Exception 'NSInvalidArgumentException'

I'm creating UIViewController that contains a UICollectionView this is all setup in the storyboard, delegates and outlets I believe to be correct. In the UIViewController I have implemented the following methods:
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
In addition to the above the storyboard includes a UICollectionViewCell that is defined in a separate class. This is all working perfectly and the CollectionView is displaying data as expected.
But... I want a create a separate class that implements UICollectionViewDataSource, UICollectionViewDelegate methods and I've cut and pasted the methods from above that were in the UIViewController class into a separate datasource class. I'm then setting the UICollectionViews delegate and datasource to point to the new class. This results in the following exception :
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'UICollectionView must be initialized with a non-nil layout parameter'
I have tried adding the new class as an NSObject in the storyboard and connecting the collection view, and also programmatically in the ViewDidLoad method in the ViewController same exception with both. Also the exception is being generated before ViewDidLoad, the ViewController is the root view controller.
Am I missing something, googling suggests that the collection view initWithCollectionViewLayout is required but how is this achieved when the CollectionView is being initialized in the StoryBoard and why does it work when it's methods are in the ViewController class?
One final point this is using xCode 6 Beta 5, in a perfect world I'd try this in xCode 5 but I'm migrating the data source to Swift.
After all that typing I discovered that it was a silly typo, I'd made the datasource class a subclass of UICollectionView instead of NSObject.

Uitableview when scrolling past a certain point

i have looked through basically every single stack overflow question on this, but i cant find an answer. None of the answers seem to work.
I am writing an application that has a tableview in it, with a uitableview as a subview.
The uitableview is datasource and delegate connected to a class called xvalues, which is a subclass of uitableviewcontroller. i also have a class, where i load a custom uitableviewcell. This all doesn't cause any errors.
Here is my code for the uitableview, in xvalues:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
tableviewcell *cell = [[tableviewcell alloc]initWithFrame:CGRectZero];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:#"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
*/
}
I removed some commented out methods such as can edit row at index path and commit editing style.
In cellforrowatindexpath, tableviewcell is my custom cell class.
From this, when i run my code, it loads up my uitableview, with 1 cell, with my custom cell.
However, now, if i click and drag, it works for dragging about an inch, and then suddenly crashes with the error:
2012-07-16 12:14:16.957 spreadsheet[68717:f803] -[__NSCFString tableView:cellForRowAtIndexPath:]: unrecognized selector sent to instance 0x6894400
2012-07-16 12:14:16.960 spreadsheet[68717:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString tableView:cellForRowAtIndexPath:]: unrecognized selector sent to instance 0x6894400'
*** First throw call stack: blah blah blah
It also happens when i click on the row, but i think that is because of my did select row at index path method.
If anyone could help me it would be amazingly helpful.
Your table view controller is getting deallocated. Make sure you’re retaining your xvalues instance (or assigning it to a strong property on something, like your app delegate) wherever you’re creating it.

Resources