Tableview refreshing to parent view after selecting child using reload data - iphone

I have a UITableView that uses JSON to to get new data from the AppDelegate. It saves the data and then is pulled into this tableview class from the AppDelegate.data3, After I add a record to the mysql database I launch the Delegate method that refreshes the data.
However,[self.tableview reLoadData]; breaks the drill down ability of the table, If I select the row, it pushes the child view for a split second and the refreshes the screen with the Parent Rows. If I take out the [self.tableview reLoadData]; The parent pushes to the child but I don't get a refreshed screen with the new data.
Any Ideas?
-(void) loadData3;{
//Initialize table data source
MyAppDelegate *AppDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
self.tableDataSource3 = [AppDelegate.data3 objectForKey:#"Rows"];
}
- (void)viewDidLoad {
[super viewDidLoad];
if(CurrentLevel3 == 0) {
self.navigationItem.title = #"Parent Table";
}
else
self.navigationItem.title = CurrentTitle3;
}
}
- (void)viewDidAppear:(BOOL)animated {
[self loadData3];
[self.tableview reloadData];
[super viewDidAppear:animated];
}

There are several issues. It's not clear what you are trying to do.
You set self.tableDataSource3 to tempArray, and then set it to [AppDelegate.data3 ....];
Why?
NSArray *tempArray = [[NSArray alloc] init];
self.tableDataSource3 = tempArray;
[tempArray release];
MyAppDelegate *AppDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
self.tableDataSource3 = [AppDelegate.data3 objectForKey:#"Rows"];
On Startup [self loadData3] gets called twice. Once in viewDidLoad and viewDidAppear. Unnecessary. Should only be in viewWillAppear.
You're either not saving data that you're adding, or not retrieving it properly. Might have to step through your code to see if you're getting the data you should be getting.

Related

App Crash on setting UITableViewCell:textLabel:text

I am creating a split-view iPad application. When the user presses the bar button item in the master view, a modal is presented. This modal has a textfield and has an IBAction to pick up keyboard returns.
On keyboard returns, a new instance of my Farm class is created (code below). This instance is then added to an array that is stored in my delegate. I then try to reload the MasterViewController's table. Upon this reload the application crashes on cell.textLabel.text with a EXC_BAD_ACCESS error.
Farm *current = [delegate.arrayOfFarms objectAtIndex:indexPath.row];
cell.textLabel.text = [current getFarmTitle];
If I ask the array within the delegate how many elements it has, it will indeed show the current amount, even. This is what is bizarre to me about this whole thing: the Farm instances appear to be in existence.
I have instances of AppDelegate in both my MasterViewController and my NewFarmNamingView classes. The instance in the Master is to populate the table. The instance in NewFarm is to add the newly created Farm to the delegate. Code below.
Segments from class NewFarmNamingView:
- (IBAction) keyboardDonePushed:(id)sender
{
// create a Farm and add it to the delegate
NSString *text = newFarmTextField.text;
Farm *newFarm = [[Farm alloc] init];
[newFarm setFarmTitle:text];
[[delegate arrayOfFarms] addObject:newFarm];
[newFarm release];
NSLog(#"Added farm: %#" , text);
// dismiss the view
[self closeView:nil];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// initialize the delegate
delegate = [[UIApplication sharedApplication] delegate];
}
Segments from the class Farm
- (void) setFarmTitle : (NSString *) _farmTitle
{
farmTitle = _farmTitle;
}
- (NSString *) getFarmTitle
{
return farmTitle;
}
// NSCoding Methods
- (void) encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:farmTitle forKey:#"kFarmTitle"];
}
- (id) initWithCoder:(NSCoder *)aDecoder
{
farmTitle = [aDecoder decodeObjectForKey:#"kFarmTitle"];
return self;
}
// Initialization method
- (id)init
{
self = [super init];
if (self) {
// Initialization code here.
}
return self;
}
From the runtime reference: "objc_msgsend sends a message to the receiver and expects a simple return value."
I'll bet you anything that what you're returning (if you're returning anything at all) in that class method getTitleFarm is returning an incorrect value. It should be an NSString. Be absolutely sure it is returning an NSString, and not anything else.
If you need to use the respondsToSelector method to see if the class is being released, try:
if([current respondsToSelector:#selector(getFarmTitles)]) {. [current getFarmTitle];
}
else {
NSLog:(#"FAILURE!!");
}
EDIT: maybe you are not retaining or even creating this string at all. In it's initialization, wrap it in a retain]; message

Scroll View inside a View

i need your help. Basically I created a small scrollView and a pageControl inside my main view controller. Now when ever a button inside a scroll view is pressed I lose the value of every property in my mainViewController. To help you get a clearer picture let me explain:
(NoteViewController.m) This is the action the button that is pressed from the scrollview responds to
- (IBAction)removePerson:(UIButton *)sender {
MainViewController *remover = [[MainViewController alloc] init];
[remover removePersonWithPage:pageNumber];
[self.view removeFromSuperview];
[remover release]; }
(MainViewController.m)
- (void)removePersonWithPage:(int)page {
// The managedObjectContext is lost the moment it leaves MainViewController.m and goes to NoteViewController.m
// so you need to reload the managedObjectContext
if (managedObjectContext == nil)
{
managedObjectContext = [(OrdersAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
}
// Get the list of people (Persons) from the managed Object Context
arrayOfPeople = [[NSMutableArray alloc] initWithArray:[self fetchDataWithEntity:#"Person" andSortKey:#"pageId"]];
// Find a specific person to delete using their page number and delete it finally remove it from array
NSManagedObject *personToDelete = [arrayOfPeople objectAtIndex:page];
[managedObjectContext deleteObject:personToDelete];
[arrayOfPeople removeObjectAtIndex:page];
// kNumberOfPages is replaced with the new number of people
kNumberOfPages = arrayOfPeople.count;
/* This is where problem occurs */
self.pageControl.numberOfPages = kNumberOfPages;
NSLog(#"The number of pages in the page control in remove is: %d", self.pageControl.numberOfPages);
[self saveObjectContext];
}
So everything works but when I get to the NSLog at the end there, it returns 0 when it ought to be returning the number of pages in the database. I've been working on this for days now and can't figure it out, please help. Thanks

AQGridview Not Calling Datasource Methods

I am trying to implement AQGridView based upon the ImageDemo in the /examples folder. I have a view controller with the following declaration:
#interface ImageDemoViewController : UIViewController <AQGridViewDelegate, AQGridViewDataSource, ImageDemoCellChooserDelegate>
{
...
None of the datasource methods in my view controller such as
- (NSUInteger) numberOfItemsInGridView: (AQGridView *) aGridView
{
return ( [images count] );
}
are being called. Here is where I setup the gridview making my view controller the delegate for the gridview.
- (void)viewDidLoad
{
[super viewDidLoad];
self.gridView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
self.gridView.autoresizesSubviews = YES;
self.gridView.delegate = self;
self.gridView.dataSource = self;
images=[[NSMutableArray alloc]init];
[images addObject:#"http://t3.gstatic.com/images?q=tbn:ANd9GcTOXAzFMoK441mcn9V0OemVe_dtAuCpGjBkLrv4rffyOjYIo45BEw"];
[self.gridView reloadData];
}
If I set a breakpoint on
[self.gridView reloadData];
the line is executed but reloadData method in AQGridView is not called. The only difference from the ImageDemo is I do not have a .xib file for the view controller. Have I forgotten to hook up something, resulting in the datasource methods not being called?
If there's no XIB, then who's creating the gridView? If it's never created, then it would be NIL, and you'd have the behavior you describe. (If that's it, then just adding:
self.gridview = [AQGridView alloc] initWithFrame: ...]; should suffice.
Had the same problem. Solved by replacing the view with the AQGridView.
[self.view addSubview:self.gridView]
self.view = self.gridView;
Full method:
- (void) viewDidLoad
{
[super viewDidLoad];
self.gridView = [[AQGridView alloc] init];
self.gridView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
self.gridView.autoresizesSubviews = YES;
self.gridView.delegate = self;
self.gridView.dataSource = self;
self.view = self.gridView;
[self.gridView reloadData];
}
Maybe you could try implementing this:
- (void)LoadSearch
{
NSURL *test1 = [NSURL URLWithString:#"http://www.4ddraws.com/search_iphone.asp"];
NSURLRequest *test = [NSURLRequest requestWithURL:test1];
[web4D setScalesPageToFit:(YES)];
[web4D loadRequest:test];
}

self.tableview reloadData causing crash in UITableView

I'm getting this crash when selecting a row: '-[__NSCFArray objectAtIndex:]: index (1) beyond bounds (1)',
I moved the data out of the viewWillAppear because we though it was causing a crash. I now have it loading on ViewDidLoad.
However if the [self.tableview reloadData]; is on, I get this crash.
Ideas?
-(void) loadData3;{
MyAppDelegate *AppDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
self.tableDataSource3 = [AppDelegate.data3 objectForKey:#"Rows"];
NSLog(#"AppDelegate.data3 : %#",AppDelegate.data3 );
NSLog(#"self.tableDataSource3 : %#",self.tableDataSource3 );
}
- (void)viewDidLoad {
[super viewDidLoad];
[self loadData3];
if(CurrentLevel3 == 0) {
self.navigationItem.title = #"Families I Follow";
}
else
self.navigationItem.title = CurrentTitle3;
}
}
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear: animated];
[self.tableview reloadData];
}
More than likely, you are changing the Array that loads the UITableView while it is being displayed, so when you click on a Row the row no longer exists in the Array. Therefore, the out of bounds error on the Array.
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self.tableView reloadData];
}
Moving reloadData to viewDidAppear solves this issue.
Since its happening while selecting a row, the error is most likely in your tableView:didSelectRowAtIndexPath: or tableView:willSelectRowAtIndexPath: method(s). Nothing seems intrinsically wrong with the viewWillAppear: code fragment that you've posted.

iPhone UISearchBar view not updating immediately?

I currently have a UISearchBar and UIDisplayController defined in my RootViewController as:
- (void)viewDidLoad {
[super viewDidLoad];
//Add the search bar
aSearchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
[aSearchBar sizeToFit];
aSearchBar.delegate = self;
aSearchBar.placeholder = #"Search YouTube...";
self.tableView.tableHeaderView = aSearchBar;
searchDC = [[UISearchDisplayController alloc] initWithSearchBar:aSearchBar contentsController:self];
[self performSelector:#selector(setSearchDisplayController:) withObject:searchDC];
searchDC.delegate = self;
searchDC.searchResultsDataSource = self;
searchDC.searchResultsDelegate = self;
[aSearchBar release];
[searchDC release];
}
When the search button is fired, this event is executed to run an API call:
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
[videoList removeAllObjects];
if (searchBar.text.length > 0) {
NSString *searchCriteria = [searchBar.text stringByReplacingOccurrencesOfString:#" " withString:#"+"];
YTJAppDelegate *appDelegate=(YTJAppDelegate*)[[UIApplication sharedApplication] delegate];
[appDelegate searchWithCriteria:searchCriteria];
}
}
The data is fetched correctly. However. It only becomes visible when I hit the 'Cancel' button on the search.
How can I get the view to update correctly the moment the data source is updated/search button is hit? Is there a special SearchDelegate method I need to implement?
the code for the associated table view might be helpful, but I am going to guess that you are not calling [self.tableView reloadData] after the search is completed
turns out the solution was to point the searchDisplayController delegates and data sources at the table view it was implementing:
searchDC.delegate = self;
searchDC.searchResultsDataSource = self.tableView.dataSource;
searchDC.searchResultsDelegate = self.tableView.delegate;