Reload data on UITableView - iphone

how to reload in UITableView class.
I am using the reloadData method but it's not working.
- (id)initWithCoder:(NSCoder *)coder
{
if (self = [super initWithCoder:coder])
{
[self init];
}
return self;
}
-(id)init
{
appdelegate=[[UIApplication sharedApplication]delegate];
[appdelegate readLiteratureFromDatabase];
tbl_obj.backgroundColor=[UIColor clearColor];
tbl_obj.delegate = self;
tbl_obj.dataSource = self;
[tbl_obj reloadData];
array_content = [[NSMutableArray alloc] init];
return self;
}
Here the init method call in another class. So at that time table are not reload.
please help!

From looking at your code it appears that you really need to read this;
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Introduction/introObjectiveC.html
You should not have a method called init that does not call up to super, that will eventually hose things.
Setting your docs ivar to an empty immutable string does nothing but waste memory. Eventually you will need to set docs to something real, wait till that point to do any allocation.
Next download the TableViewSuite sample from here;
http://developer.apple.com/library/ios/#samplecode/TableViewSuite/Introduction/Intro.html%23//apple_ref/doc/uid/DTS40007318
And take a look at the simple sample.
Please take the time to study that sample, it will help you understand table views.

May be that you might not have attached DataSource and Delegate to your XIB
or
You can do it through code using:
self.tableView.delegate = self;
self.tableView.datasource = self;
Also you have to add it as below:
#interface <YourViewControllerName>:UIViewController <UITableViewDelegate,UITableViewDataSource>
EDIT:
But you should try and understand that your controls like UITableView cannot be directly called and set into your UIView class.
It should be UIViewController only then it is possible to add and fillup the data into the TableView.
So your option is to call the data delegate and datasource methods and also the reloadData on the ViewController which works as parentView for the this UIView.
So unless you do this way, I fear that it is not possible.
Hope this helps you.

UIView does not have a reloadData method. UIView Class Reference
Maybe you can show your code so we can see what you are trying to do?

I had the same prob and i just used the viewDidLoad to add the data and in the end i used [super loadView]; and it reload.

Related

passing value in init method

I am a new programmer of Iphone Application. I work dynamically not use nib files.
I want to pass values from one view controller to another.
in firstView.m
these two values
cell.textLabel.text
cell.detailTextLabel.text
i want to pass in the TabBarSearchThirdView init method..
TabBarSearchThirdView *thirdView=[[TabBarSearchThirdView alloc]init];
[myView addSubview:thirdView.view];
in TabBarSearchThirdView.m file.
-(id)init
{
firstVariable=..........
secondVariable=..........
}
Hope you understood my question. Give me little code or suggestions if possible
and give me a link of easy and good table view tutorial. If possible
Thanks in advance
You should declare a new init method like this one
- (id)initWithText:(NSString *)aText andDetail:(NSString *)aDetail{
self = [super init];
if(self){
firstVariable = aText;
secondVariable = aDetail;
}
return self;
}
For passing data between viewcontrollers my answer at the below link will help.
How to pass a tableview cell value to another page(uiviewcontroller)?
for passing values from 1 controller to another controller, you have to define 2 variables in AppDelegate class, with property and synthesis . example NSstring *firstVariable and NSString *secondVariable and mention its property and synthesis.
and after your code
TabBarSearchThirdView *thirdView=[[TabBarSearchThirdView alloc]init];
MyAppnameDelegate AppDel = (MyAppnameAppDelegate*)[[UIApplication sharedApplication]delegate];
//MyAppnameDelegate is your appdelegete class, so change it by your appDelegate class
AppDel.firstVariable =cell.textLabel.text;
AppDel. secondVariable = cell.detailTextLabel.text;
[myView addSubview:thirdView.view];
process to access values in Third class is
In viewDidLoad{
MyAppnameDelegate AppDel = (MyAppnameAppDelegate*)[[UIApplication sharedApplication]delegate];
//MyAppnameDelegate is your appdelegete class, so change it by your appDelegate class
nsstring *accessfirstVariable =.AppDel.firstVariable
nsstring *accesssecondVariable =AppDel. secondVariable
}
and use this where ever u likeā€¦ cheers

Why does myArray not get initialized?

- (id)initWithStyle:(UITableViewStyle)style {
// Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
self = [super initWithStyle:style];
if (self) {
myArray = [[NSMutableArray alloc]init];
NSLog(#"ARRAY INITIALIZED"); //THIS NEVER OCCURS
}
return self;
}
Are you creating your table programmatically or is it in Interface Builder? If it's in interface builder, then you need to override -(id) initWithCoder:(NSCoder *)coder instead.
I would recommend just putting your initialization code in viewDidLoad or viewDidAppear. You would need to make sure you don't alloc multiple times (and orphan memory in the process), but it would get called regardless of whether this is rigged up in the xib or programmatically.
- (void)viewDidLoad
{
if(!myArray)
{
myArray = [[NSMutableArray alloc] init];
}
}
I can't really tell from your given code, but if you're making your controller programmatically, make sure you're initializing your subclass:
[[MyCustomTableViewController alloc] initWithStyle:style];
Rather than UITableViewController:
[[UITableViewController alloc] initWithStyle:style];
Found out that initWithStyle never gets called. I forgot to mention, the initial window is build in IB, so my custom controllers are called from the xib, and i have to override initWithCoder....
http://www.iosdeveloperforums.com/thread-initwithstyle-overriding

EKEventViewDelegate didCompleteWithAction not getting called

I don't get a call to my eventViewController:didCompleteWithAction: when the EKEventViewController finishes edting an event.
Here's how I set it up:
- (void)showCalendar:(id)sender {
EKEventViewController *eventViewController = [[EKEventViewController alloc] init];
eventViewController.delegate = self;
eventViewController.event = self.event;
// Allow event editing.
eventViewController.allowsEditing = YES;
[self.navigationController pushViewController:eventViewController animated:YES];
[eventViewController release];
}
I do have the protocol on my class and the method was implements by copy and pasting the definition from the docs. It just doesn't get called.
If I use the EKEventEditViewController and its corresponding delegate, then that does get called when the event is saved.
I was able to reproduce the problem in the SimpleEKDemo code same as well. Does anyone know what might be wrong?
I could just drop the view functionality and go straight to the EKEventEditViewController, but I'd rather not.
Might be a bit late to be helpful, but I had this problem as well.
To get around it I subclassed EKEventViewController, then in the subclass' viewDidLoad I replaced the standard edit button with one of my own:
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *editItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self.delegate action:#selector(editCalEvent)];
self.navigationItem.rightBarButtonItem = editItem;
}
That way when you want to edit an event, you can set up your own EKEventEditViewController and specify its delegate in order to respond to changes:
- (void)editCalEvent {
EKEventEditViewController *editController = [[EKEventEditViewController alloc] init];
editController.event = editingEvent;
editController.eventStore = self.eventStore;
editController.editViewDelegate = self;
[self presentModalViewController:editController animated:YES];
[editController release];
}
Hope that helps.
I had the similar problem when I use "pushViewController", the result is that it will go to
- (void)navigationController:(UINavigationController *)navigationController
willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{}
But after I changed to presentModalViewController, it will go to eventViewController:didCompleteWithAction: when Done/Cancel/Delete are pressed.
in this .m file you need to import the EventKit/EventKit.h and EventKitUI/EventKitUI.h
and in the .h file you need to implement the 'EKEventViewDelegate' delegates.
hope it helps you
This does seem to be a fairly obvious omission in the library. My workaround: I'm presenting the EKEventViewController in a UINavigationController. I detect completion in the viewWillAppear method of the controller than pushed the EKEventViewController onto the view stack. Use a boolean variable in this view controller to track and differentiate between initial appearance and re-appearance due to the EKEventViewController being popped. There is a risk that your code will get called at other times, but if you are just refreshing tableviews, etc, then this should be sufficient.

Messaging between ViewControllers Using Delegates

I'm trying to send an array from one viewController to another using protocols and delegates. I declared the delegate on viewcontroller B and used the following code in viewcontroller A to send the message from A to B. The protocol's method is didReceiveMessage. Unfortunately, the message never arrives.
Attached is the code from viewController A
- (IBAction) graphPressed:(UIButton *)sender {
GraphingViewController *gvc=[[GraphingViewController alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:gvc animated:YES];
[gvc release];
[delegate didReceiveMessage:brain.internalExpression];
}
and the code from viewcontrollerB
- (IBAction) ViewdidLoad {
CalculatorViewController *cvc =[[CalculatorViewController alloc] init];
cvc.delegate=self;
[cvc release];
}
- (void) didReceiveMessage:(NSMutableArray *)expression {
NSLog(#"message received from CalculatorAppDelegate");
}
Any suggestions would be greatly appreciated.
I'm not sure what are you doing in second sample? You created an object, assign its delegate property and then released it. Why?
If that is code from your application it could probably be from releasing cvc at the end of your ViewDidLoad method.
init would give it a retain count of 1, and the release would take it back to 0.
Also the code seems sort of backwards, if you wanted to set view A as the delegate for view B, you would do so in view A.
Unless there is a more complex reason to use a delegate that I'm not seeing from the code, I would just keep a pointer around to the child if you really need to send it messages.
Like others have posted, you are just getting rid of the Calculator VC after creating it. I would recommend adding an #property to the second VC and using it to store a pointer to the Calculator. It should be a retain property. Then set the delegate of that property to self.
Also, you make sure to use an assign property for the delegate value, and try to use the property syntax (self.delegate) whenever possible.
There could absolutely be more going on here, so if this doesn't actually solve the problem try and post up more of your code (header files, what connections are made in IB, etc.)
Edit: For the record, the method is -(void)viewDidLoad, not -(void)ViewDidLoad, so that could be contributing to the problem.
As you said you are trying to pass an array from one view controller to another.Well i use in this way.Here is the code
- (IBAction) graphPressed:(UIButton *)sender {
GraphingViewController *gvc=[[GraphingViewController alloc] initWithNibName:nil bundle:nil];
gvc.myArray=infoArray;
[self presentModalViewController:gvc animated:YES];
[gvc release];
}
Where myArray is array in your GraphingViewController,You just need to make property of this array with retain attribute,simply as
#property(nonatomic,retain)NSMutableArray *myArray;
And you need to Synthesize it as well.

How can one create a generic (container)controller that will recieve as input another controller

In the app im creating there are many pages that look mostly the same with some part which is different. To handle this kind of situation i created a container controller that contains a subview. I want this subview to be filled by the contents of another controller (and its associated nib) which i will created dynamically as needed based on context.
I have the following method somewhere
- (void) someAction {
UIViewController* contentController = [[MyContentController alloc] init];
UIViewController* containerController = [[MyContainerController alloc] initWithContentController:contentController];
[navigationController pushViewController:pageController animated:YES];
[contentController release];
[containerController release];
}
In MyContainerController.m i retain the controller in a property
- (id)initWithContentController:(UIViewController *)aContentController {
if ((self = [super initWithNibName:#"MyContainerController" bundle:nil])) {
contentController = aContentController;
}
return self;
}
Later in viewDidLoad i do the following
- (void)viewDidLoad {
[super viewDidLoad];
[contentViewContainer addSubview:contentController.view];
}
contentViewContainer is the view that's supposed to hold the page specific info.
Unfortunatly this fails with EXC_BAD_ACCESS.
The funny thing is that if i alloc and init the content controller from within viewDidLoad everything works. It seems that i cant pass a contoller i allocated from another place.
Can anyone assist.
Since you are releasing contentController in the actionMethod
you have to retain contentController in you init method
- (id)initWithContentController:(UIViewController *)aContentController {
if ((self = [super initWithNibName:#"MyContainerController" bundle:nil])) {
contentController = [aContentController retain];
}
return self;
}
But, why do you need this? Controllers are supposed to control views and no other controllers. If you think you really need that then you want to use UINavigationController or UITabBarController maybe.
You can also load views without a controller (see here)
I personally think that having UIViewControllers inside of simple UIViewController is not a preferable approach
Hope it helps