Reloading a TableView's data? - objective-c

I'm trying to build a hello world application with Xcode 4.3.
So far, I have a TableView (dragged and dropped from the objects list) and a TableViewCell (also dragged and dropped from the objects list).
I have an array too, and my TableView works and gets the data from array, but how can I reload the TableView's data? I tried [self.tableView reloadData]; but it doesn't work. When I type [self., tableView is not in my options...

To have the NSTableView as a property of your class, you have to declare it as one.
In the interface of your class add:
#property (weak) IBOutlet NSTableView *tableView;
In the implementation add:
#synthesize tableView;
Then in Interface Builder connect the table view to the tableView outlet of your class, by right clicking on "File's Owner" and dragging a line from "tableView" to your table view.

Related

NSObject as delegate for UITableView

I have a UITableView which is dragged into a UIViewController and a NSObject class. This NSObject class should serve as delegate and datasource to the tableview.
I tried creating an instance of NSObject and conformed delegate and datasource to the tableview. Datasource is working fine. But the cellForRowAtIndexPath: is never getting called and all i get is a EXC_BAD ACCESS and app just stops right there. But when I drag a object to UIViewController bottom pane and giving the class name to it and setting delegate and datasource, everything is working good and UITableView is displaying.
I don't know why this strange behaviour.
I have done this in the viewdidload of viewcontroller class.
customnsobject *custom=[customnsobject alloc]init];
tableview.delegate=custom;
tableview.datasource=custom;
create a parent id in your class h file like this
#property(nonatomic,retain) id parent;
and in your view controller just below the instance of nsobject you created do this
custom.parent=self;
I hope this will work

UITableView within a UIViewController - Data doesn't load

I know that there are several posts around the www, but sadly I don't get the tableView to be loaded.
here is my code:
.h file
#interface ViewControllerSzenenLichter : UIViewController<UITableViewDelegate, UITableViewDataSource>{
IBOutlet UITableView *tableView;
}
#property (nonatomic, retain) IBOutlet UITableView *tableView;
#end
I connected the IBOutlet with my tableview in the Storyboard
.m file
#interface ViewControllerSzenenLichter ()
#end
#implementation ViewControllerSzenenLichter
#synthesize tableView;
somewhere in the code I try to execute
[self.tableView reloadData]
which is executed(checked by Breakpoint)
but the method numberOfRowsInSection or numberOfSectionsInTableView are never called!
As mentioned I found a lot of tutorials in the web, but I can't solve the Problem.
thanks
Your child tableView is basically a cell of your parent tableView.
So, unless you reload your parent tableView, the cell will not update, and hence, the child tableView will not be plotted.
[childTableView reloadData];
[parentTableView reloadData];
EDIT :
And #Desdenova is right. Make sure you set your delegate and data source to self or whatever class manages the tables.
In the storyboard Right click over the table you will see datasource and delegates as the first two options.Connect both to the viewcontroller (same as you did for outlet connection) and run again.
The final table connections will be like this
It seems, you connect one IBOutlet and use another when reload data: You have two ivars tableView (you create it manualy) and _tableView (created compiler and links with #property (nonatomic, retain) IBOutlet UITableView *tableView;), I don't now which one will be shown as TableView property of file owner in InterfaceBuilder. Try remove IBOutlet UITableView *tableView; ivar and reconnect Table in IB
First of all, make sure you are setting delegate in xib file or implementation file for your table. If still not working, check the code where you are reloading table. If main thread is busy doing something else, it will not call any of the delegate method of UITableView. So when the code finishes, then reload tableview.
Connect your TableView to the File's Owner by the delegate and datasource...
I think your problem will be solved. Here is a picture for your batter understanding...

Using a protocol and a delegate

I am trying to get some code in a view working. I have declared a delegate and it does not get instantiated, any Idea what I am missing?
I have tried to summarise how I have done this below. I think that the issue is that somewhere, my dataSource delegate needs to be instantiated.
I have a View called graph view and a delegate that is in the viewcontroller GraphViewController.
I know that the method in GraphView is doing something as it calls the AxisDrawing helper class and draws in Axes.
Here is the relevant code
In GraphView.h I set up the protocol and the dataSourceDelegate
#class GraphView;
#protocol GraphViewDataSourceDelegate
- (float)functionOfX:(float)xValue inGraphView:(GraphView *)sender;
#end
#interface GraphView : UIView
#property(nonatomic, weak) IBOutlet id <GraphViewDataSourceDelegate> dataSourceDelegate;
#end
Which I synthesize in GraphView.m
#import "GraphView.h"
#import "AxesDrawer.h"
#implementation GraphView
#synthesize dataSourceDelegate = _dataSourceDelegate;
Also in Graph View I try to use the delegate as follows (pixel is a CGPoint). This routine does run and I can draw to GraphView from here provided I do not try to use my protocol method. i.e. Some of my DrawRect stuff does get drawn which checks out the linking of the UIView to my custom View
pixel.y = [self.dataSourceDelegate functionOfX:pixel.x inGraphView:self];
++++Breakpoint put in here+++++
In GraphViewController I state that I implement the protocol and implement it as follows. The compiler warns me and spots when the implementation is done, turning of the warning. (I am only returning 3.0 at the moment as a test).
#interface
GraphViewController () <GraphViewDataSourceDelegate>
#property (nonatomic, weak) IBOutlet GraphView *graphView;
#end
...
-(float) functionOfX:(float)xValue inGraphView:(GraphView *)sender{
NSLog(#"fofx accessed");
return 3.0;
}
If I look at the GraphView* object just after a breakpoint, it seems to have no instance. What am I missing out.
This is from the trace at the breakpoint
_dataSourceDelegate struct objc_object * 0x0
EDIT: (In addition to #Clays answer below)
It turned out that my connection to the custom view was broke. This meant that the fault lay with the View not talking to the Custom ViewController. The link is made by ctrl dragging from the View Controller button in the Interface builder to the View within the ViewController.
This caused the ViewController to be instantiated and everything then worked fine.
To put it another way.
The link in question was the Outlet. This has to be declared in the ViewController as a property and then linked in IB using ctrl Drag from the ViewController Name-Bar to the View in the Controller.
Provided that you have added the Outlet property correctly, when you do the ctrl drag, your view will appear an option.
The context button popup information on the ViewController button in IB does give a clue.
If you have neglected to put the outlet property into the view controller, the link does appear in the context button popup but it is greyed out, and when you do the link your View is not named.
Once you put the outlet in, the name appears in the menu but not greyed out.
Make sure you've hooked everything up correctly: setting your GraphView's dataSourceDelegate, and your GraphViewController's graphView.
From the trace it looks like you haven't done that; or you lose the reference somewhere along the way because at some point nothing's holding on to it.

Why can I not wire up my IBOutlets in storyboard?

I have a ViewController that has a couple of IBOutlet properties defined for UITextView. In the storyboard I have assigned my viewcontroller to the custom view controller. When I cntr + click on the viewcontroller I can see my two IBOutlets. When I try to drag them to the UITextViews on the storyboard they will not highlight and cannot be assigned.
I'm new to xcode 4.
Here is the property definitions from the viewcontroller:
#interface CreateUserViewController : UIViewController<UITextInputDelegate>
#property(nonatomic,retain) KeychainItemWrapper *keyChainWrapper;
#property(nonatomic,retain) IBOutlet UITextView *userNameTxt;
#property(nonatomic,retain) IBOutlet UITextView *passwordTxt;
These properties are synthesized in the implementation. I can assign each of the textfields' delegate to the viewcontroller without a problem. Am I missing any step?
Thanks
Make sure that the views you're trying to connect really are UITextViews and not UITextFields. It's not difficult to get them mixed up.

XCode IBOutlet UIView added to ViewControllers .h -- UIViews type not recognized

I have added the view of my view controller as a property to the view controllers backing .h file myViewController.h:
#property (weak, nonatomic) IBOutlet UIView* view;
When writing view.window inside one of myViewController.m methods, Xcode marks that line as an error with "property 'window' not found on object of type view". It fails to build. Note I originally chose the views backing class view.h as the type. I already clean ed the project.
Specifically I get the error here:
- (void)viewDidLoad
{
[super viewDidLoad];
displayLink = [view.window.screen displayLinkWithTarget:(self) selector:#selector(drawFrame)];
}
How can I resolve this? I like to access the view controllers views window inside one of the view controllers methods.
What is the #synthesize statement?
Don't name an outlet "view", that causes confusion with UIViewController that has a view property.
Currently Apple defaults to making iOS IBOutlets weak. This has changed since the early iOS SDKs. IBOutlets that are weak will generally work if they are retained by a super view or controller.