I have a tableview in the midddle of my tab bar template application..
I wanted to add the contents of the NSMutableArray called 'routines'.
Here is my .h file
#import <UIKit/UIKit.h>
#interface FirstViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
NSMutableArray *routines;
}
#property (nonatomic, retain) NSMutableArray *routines;
- (IBAction)showNewEventViewController;
#end
and my .m file.
#import "FirstViewController.h"
#import "NewEventViewController.h"
#implementation FirstViewController
#synthesize routines;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [routines count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell...
NSString *cellValue = [routines objectAtIndex:indexPath.row];
[cell.textLabel setText:cellValue];
return cell;
}
and viewDidLoad method
- (void)viewDidLoad {
routines = [[NSMutableArray alloc] init];
[routines addObject:#"Hello"];
[routines addObject:#"Temp"];
[routines addObject:#"Temp2"];
[routines addObject:#"Temp3"];
[routines addObject:#"Temp4"];
self.navigationItem.title = #"test";
}
My objects are just not displaying. As you can see, i have Added
and i have hooked it all up in IB correctly.
When I try to open my app ( return) it crashes, and spits out the following log.
[Session started at 2010-01-19 17:57:01 +1300.]
2010-01-19 17:57:03.563 Gym Buddy[12690:207] *** -[UITabBarController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x3b12450
2010-01-19 17:57:03.564 Gym Buddy[12690:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[UITabBarController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x3b12450'
2010-01-19 17:57:03.577 Gym Buddy[12690:207] Stack: (
29295707,
2538743049,
29677627,
29247094,
29099714,
4364410,
4371786,
4370783,
3087322,
3027833,
3069268,
3057823,
55808688,
55808111,
55806150,
55805242,
2731769,
2755464,
2737875,
2764981,
37392081,
29080448,
29076552,
2731625,
2768899,
9784,
9638
)
I have no idea what is going wrong, since im a bit of a newbie.
Thanks Guys!
Sam
It looks like you've assigned the datasource of your table to be your UITabBarController, rather than your FirstViewController object. That second line of your pasted error message is saying it's trying to get the numberOfRows, but its datasource doesn't implement it. Double check your connections in IB.
Related
I have a nsmutablearray which im trying to display on a table view. i have a uiviewcontroller and added the table view manually. i have added the delegate and source on the xib by draggin it to the file owner. here is my .h file
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>
{
IBOutlet UILabel *label;
IBOutlet UITextField *texto;
IBOutlet UIWebView *link;
//IBOutlet UILabel *links;
IBOutlet UITextView *links;
// IBOutlet UITableView *tableView;
NSMutableArray *jsonArray;
}
#property (nonatomic,retain) IBOutlet UITableView *tableView;
//#property (nonatomic,retain) NSMutableArray *jsonArray;
-(IBAction)button;
-(IBAction)field;
-(void)populateArray;
#end
and here is my .m file
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
NSString *one;
NSString *jsonreturn;
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:#"www.myphpfile.com"];
jsonreturn = [[NSString alloc] initWithContentsOfURL:url]; // Pulls the URL
NSLog(jsonreturn); // Look at the console and you can see what the restults are
NSData *jsonData = [jsonreturn dataUsingEncoding:NSUTF32BigEndianStringEncoding];
NSError *error = nil;
jsonArray = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers|NSJSONReadingAllowFragments error:&error];
NSLog(#"jsonList: %#", jsonArray);
if(!jsonArray)
{
NSLog(#"Error parsing JSON:%#", error);
}
else
{
// Exception thrown here.
for(NSDictionary *item in jsonArray)
{
NSLog(#"%#", item);
}
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [jsonArray count];
}
- (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];
}
NSLog(#"Im HERE!!!");
cell.textLabel.text = [jsonArray objectAtIndex:[indexPath row]];
NSLog(#"Cell is %#", [jsonArray objectAtIndex:indexPath.row]);
return cell;
}
my question is, i cant get the cells to show the date, i have the ns logs and it shows that it is pulling it, also im getting a thread 1, breakpoint 1.1 error where the the cell is created and a warning on the same line sayin tableView is local and hiding instance variable, any thoughts? thanks for the help!
update:
I got my program to compile but it throws an exception:
2013-03-26 22:28:48.691 Hello[79888:11303] * Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key tableView.'
does this have to do with the information im givin the table or im creating the table wrong? thanks again!
If you've added the tableview through the interface builder/storyboard try adding:
#synthesize tableView;
under your #implementation in your .m file. Then make sure you have the tableview connected properly in your IB. (To the View Controller as both data source and delegate and then form the VC to tableview as tableView.)
NSString *one;
NSString *jsonreturn;
The two statements lack standardization.
If you have not using ARC, jsonreturn will leak.
You are getting the warning because you have an instance variable called tableView and also inside the table view datasource methods tableView is passed in as a parameter. Changing this variable name or the instance variable name would solve the problem. Since they both are referring to the same thing, it doesn't matter.
And if its just stopping at a point saying Stopping at Breakpoint 1 you probably have set a breakpoint. Its looks like a blue arrow mark , located on the left hand side of the code. You can disable it by clicking on it again(becomes a lighter shade) or by deleting it- drag the breakpoint out or right click on the breakpoint and delete.
look at the variable name of your tablview
IBOutlet UITableView *tableView
its same as in built method of UITableView uses
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
thats why its giving you warning, "tableView is local and hiding instance variable"
to avoid it just change your UITableView variablename to something "myTableView"
First of all allocate your NSMutableArray, Like this " ArrayName = [[NSMutableArray alloc] init];
I'm trying to create a TableView and put in data from a NSArray. However, I am getting this error:
2012-12-21 15:53:10.239 Arr[13219:c07] -[UIView tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x71ccb60
2012-12-21 15:53:10.240 Arr[13219:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x71ccb60'
*** First throw call stack:
(0x1c90012 0x10cde7e 0x1d1b4bd 0x1c7fbbc 0x1c7f94e 0x1f96e8 0x1fc3c4 0xc0fa2 0xc092c 0xc4426 0xc90ce 0x6592d 0x10e16b0 0x228cfc0 0x228133c 0x228ceaf 0x1048cd 0x4d1a6 0x4bcbf 0x4bbd9 0x4ae34 0x4ac6e 0x4ba29 0x4e922 0xf8fec 0x45bc4 0x45dbf 0x45f55 0x4ef67 0x12fcc 0x13fab 0x25315 0x2624b 0x17cf8 0x1bebdf9 0x1bebad0 0x1c05bf5 0x1c05962 0x1c36bb6 0x1c35f44 0x1c35e1b 0x137da 0x1565c 0x1c2d 0x1b55 0x1)
libc++abi.dylib: terminate called throwing an exception
I am not really sure what code is causing this, so I'll just attach the two table methods:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [tableData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
return cell;
}
Creation of the NSArray:
#implementation ArrViewController {
NSArray *tableData;
}
#synthesize itemTxt = _itemTxt;
#synthesize itemsLabel = _itemsLabel;
#synthesize model = _model;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
tableData = [NSArray arrayWithObjects:#"Egg Benedict", #"Mushroom Risotto", #"Full Breakfast", #"Hamburger", #"Ham and Egg Sandwich", #"Creme Brelee", #"White Chocolate Donut", #"Starbucks Coffee", #"Vegetable Curry", #"Instant Noodle with Egg", #"Noodle with BBQ Pork", #"Japanese Noodle with Pork", #"Green Tea", #"Thai Shrimp Cake", #"Angry Birds Cake", #"Ham and Cheese Panini", nil];
}
You have improperly set the data source for your table view. This makes the tableView:numberOfRowsInSection message to be sent to the wrong object, a UIView as it appears from the error message:
[UIView tableView:numberOfRowsInSection:]
Review/post the code where you set the dataSource property of your UITableView to find out what is wrong. You are likely passing in the wrong object.
The data source is the very object in whose class you define - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section as per your post.
in Your .h file Use the Following Code
#interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
#property (weak, nonatomic) IBOutlet UITableView *your_Table;
in Your .m file
#implementation ViewController
#synthesize your_Table;
in Xib File Connect The Delegate and dataSource to Fileowner.
or in ViewDidLoad Assign it programmatically
your_Table.delegate=self;
your_table.dataSuorce=self;
After That use this
- (NSInteger)tableView:(UITableView *)myTableView numberOfRowsInSection:(NSInteger)section {
return [tableData count];
}
Hope it will Help you.
If not, Then post your Code from .h file.
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?
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;
Question:
I'm trying to access a variable from the UITableView tableView:didSelectRowAtIndexPath: delegate method. The variable can be accessed from the data source methods, but when I try to access it from delegate methods such as this one, the app crashes.
I declare the variable in my .h file, and initialise it in .m file in the applicationDidFinishLaunching method. I haven't declared any accessors/mutators.
The weird thing is that the problem doesn't occur if I declare the variable like this:
helloWorld = #"Hello World!";
...but it does if I declare the variable like this:
helloWorld = [NSString stringWithFormat: #"Hello World!"];
Any ideas on what may be going on here? What am I missing?
Full code:
UntitledAppDelegate.h:
#import <UIKit/UIKit.h>
#interface UntitledAppDelegate : NSObject <UIApplicationDelegate, UITableViewDelegate, UITableViewDataSource> {
UIWindow *window;
NSString *helloWorld;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#end
UntitledAppDelegate.m:
#import "UntitledAppDelegate.h"
#implementation UntitledAppDelegate
#synthesize window;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
helloWorld = [NSString stringWithFormat: #"Hello World!"];
NSLog(#"helloWorld: %#", helloWorld); // As expected, logs "Hello World!" to console.
[window makeKeyAndVisible];
}
- (void)dealloc {
[window release];
[super dealloc];
}
- (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 = #"Row";
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"helloWorld: %#", helloWorld); // App crashes
}
#end
You need to retain your helloWorld instance variable. Try this:
helloWorld = [[NSString stringWithFormat: #"Hello World!"] retain];
It worked in the first instance because static strings are 'infinitely retained', and so are never deallocated. In the second, your instance variable is being released once the event loop runs. Retaining it will prevent this.