What i have so far is
#synthesize txtCountry,txtState;
int flgTextField;
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
[pickerView reloadAllComponents];
// Make a new view, or do what you want here
if(textField == txtCountry || textField == txtState){
flgTextField = textField.tag;
[UIView beginAnimations:nil context:NULL];
//[pvState setFrame:CGRectMake(0.0f, 199.0f, 320.0f, 216.0f)];
[UIView commitAnimations];
return NO;
}
else {
return YES;
}
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
if(flgTextField==1){
return [arryCountry count];
}
else {
return [arryState count];
}
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if(flgTextField==1){
return [arryCountry objectAtIndex:row];
}
else{
return [arryState objectAtIndex:row];
}
}
- (void)viewDidLoad {
arryCountry = [[NSMutableArray alloc] init];
arryState = [[NSMutableArray alloc] init];
[arryCountry addObject:#" 100 "];
[arryCountry addObject:#" 200 "];
[arryCountry addObject:#" 400 "];
[arryCountry addObject:#" 600 "];
[arryCountry addObject:#" 1000 "];
[arryState addObject:#" a "];
[arryState addObject:#" b "];
[arryState addObject:#" c "];
[arryState addObject:#" d "];
[arryState addObject:#" e "];
[super viewDidLoad];
}
in my .m
and
#interface Contact : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource> {
IBOutlet UITextField *txtCountry;
IBOutlet UITextField *txtState;
NSMutableArray *arryCountry;
NSMutableArray *arryState;
UIPickerView *pickerView;
}
#property(nonatomic,retain) IBOutlet UITextField *txtCountry;
#property(nonatomic,retain) IBOutlet UITextField *txtState;
in my .h file
Now the text fields are not editable and I need some help or guidance, or any tutorial on how to connect UIPicker with multiple sources that can be change when text fields are editing
So i see no one cares :)
what i have now is 3 textFields and whenever i touch textField1 or textField2 Picker changes values and there is no keyboard. When i touch textField3 keyboard appears and the picker goes hidden.Now if i dismiss the keyboard by clicking return and then click textField1 picker appears again, but if i dont dismiss the keyboard BY CLICKING BUTTON it stays over the picker. What I need is when the keyboard is firstResponder (and i see it on the screen) to hide it if i click on the textField1 and only to see the picker
int variabla;
-(void)textFieldDidBeginEditing:(UITextField *)textField{
[pickerView setHidden:YES];
if (textField1.editing == YES) {
[textField1 resignFirstResponder];
[pickerView setHidden:NO];
variabla = 1;
}else if (textField2.editing == YES) {
[textField2 resignFirstResponder];
[pickerView setHidden:NO];
variabla = 2;
}
NSLog(#"variabla %d",variabla);
[pickerView reloadAllComponents];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{
if (variabla == 1) {
return [pickerArray1 count];
}else if (variabla == 2) {
return [pickerArray2 count];
}else {
return 0;
}
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
if (variabla == 1) {
return [pickerArray1 objectAtIndex:row];
}else if (variabla == 2) {
return [pickerArray2 objectAtIndex:row];
}else {
return 0;
}
}
- (void)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
}
- (void)viewDidLoad {
[super viewDidLoad];
[pickerView setHidden:YES];
pickerArray1 = [[NSMutableArray alloc] initWithObjects:#"0", #"1", #"2", nil];
pickerArray2 = [[NSMutableArray alloc] initWithObjects:#"3", #"4", #"5", nil];
}
Related
I have several CoreDataTableViewControllers that utilize the helper class from Paul Hegarty's course. Everyone of them works except this one, and I cannot see a difference.
When the table first comes up, it is correctly populated and the segue executes properly when a cell is selected. However when I hit the back button, the table displays (null), (null) everywhere.
I have tried every variant of calling [self useDocument] that I can think of, still to no avail. Any thoughts? Thanks in advance.
//
// TeamTableViewController.m
//
#import "TeamTableViewController.h"
#import "iTrackAppDelegate.h"
#import "CoreDataTableViewController.h"
#import "SchoolRecords.h"
#import "ScheduleViewController.h"
#interface TeamTableViewController ()
#property NSInteger toggle;
#end
#implementation TeamTableViewController
#synthesize iTrackContext = _iTrackContext;
#synthesize schoolSelected = _schoolSelected;
-(void) setSchoolSelected:(SchoolRecords *)schoolSelected
{
_schoolSelected = schoolSelected;
}
-(void) setITrackContext:(NSManagedObjectContext *)iTrackContext
{
if(_iTrackContext != iTrackContext){
if (!iTrackContext) {
MyCoreDataHandler* cdh =
[(iTrackAppDelegate *) [[UIApplication sharedApplication] delegate] cdh];
_iTrackContext = cdh.context;
} else {
_iTrackContext = iTrackContext;
}
}
[self useDocument];
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)setupFetchedResultsController // attaches an NSFetchRequest to this UITableViewController
{
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:#"SchoolRecords"];
// no predicate because we want ALL the Athletes
request.sortDescriptors = [NSArray arrayWithObjects:
[NSSortDescriptor sortDescriptorWithKey:#"schoolName" ascending:YES],
nil];
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
managedObjectContext:self.iTrackContext
sectionNameKeyPath:nil
cacheName:nil];
__block NSInteger myCount;
int64_t delayInSeconds = 5.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[self.iTrackContext performBlock:^(void){NSError* requestError = nil;
myCount = [self.iTrackContext countForFetchRequest:request error:&requestError];
NSLog(#"In %# and count of iTrackContext = %lu", NSStringFromClass([self class]),(unsigned long)myCount);
}];
if (!myCount || myCount == 0) {
[self displayAlertBoxWithTitle:#"No Teams" message:#"Have you added athletes yet? \nPlease go to Add Athletes" cancelButton:#"Okay"];
}
});
}
- (void)useDocument
{
if (self.iTrackContext) {
[self setupFetchedResultsController];
} else {
NSString* errorText = #"A problem arose opening the search results database of Athletes.";
[self displayAlertBoxWithTitle:#"File Error" message:errorText cancelButton:#"Okay"];
}
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if (!self.iTrackContext) {
MyCoreDataHandler* cdh =
[(iTrackAppDelegate *) [[UIApplication sharedApplication] delegate] cdh];
[self setITrackContext:cdh.context];
} else {
NSLog(#"In %# of %#. Getting ready to call useDocument",NSStringFromSelector(_cmd), self.class);
[self useDocument];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// If divide into sections use line below otherwise return 1.
// return [[self.fetchedResultsController sections] count];
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Do not really need this with only one section, but makes code usable if add sections later.
return [[[self.fetchedResultsController sections] objectAtIndex:section] numberOfObjects];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"teamInformation";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
SchoolRecords *schoolResults = [self.fetchedResultsController objectAtIndexPath:indexPath];
NSString* titleText = schoolResults.schoolName;
cell.textLabel.text = titleText;
cell.detailTextLabel.text = [NSMutableString stringWithFormat:#"%#, %#", schoolResults.schoolCity, schoolResults.schoolState];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
# pragma navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
[self setSchoolSelected:[self.fetchedResultsController objectAtIndexPath:indexPath]];
// be somewhat generic here (slightly advanced usage)
// we'll segue to ANY view controller that has a photographer #property
if ([segue.identifier isEqualToString:#"scheduleDetailSegue"]) {
// use performSelector:withObject: to send without compiler checking
// (which is acceptable here because we used introspection to be sure this is okay)
NSLog(#"Preparing to passing school with schoolID = %#", self.schoolSelected.schoolID);
[segue.destinationViewController convenienceMethodForSettingSchool:self.schoolSelected];
}
}
- (void) displayAlertBoxWithTitle:(NSString*)title message:(NSString*) myMessage cancelButton:(NSString*) cancelText
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message:myMessage
delegate:nil
cancelButtonTitle:cancelText
otherButtonTitles:nil];
[alert show];
}
#end
Well, I am not certain what the problem was. I ended up deleting the "offending" TableViewControllers in StoryBoard and recreated them. That did the trick. In retrospect, I wonder if I did not specify the wrong type of segue from my tabViewController. But I deleted it before I thought of that possibility.
Ok guys. This one is driving me up the wall. I have
UIManagedDocument and its 2 MOContexts (regular and parent.)
A UITableViewController (subclassed to CoreDataTableViewController by Paul Hegarty) that runs off of an
NSFetchedResultsController
A background GCD Queue for syncing with the server that the parent cue accesses
I've tried this so many different ways and I run into problems each time.
When I add a new "animal" entity, it is no problem and immediately shows up on the table. But when I upload it to the server (on the upload queue) and changed its "status" (with the parent context) so that it should be in the uploaded section, it appears there but doesn't disappear from the un-uploaded section.
I END UP WITH TWINS I DIDN'T WANT! or it doesn't even make the correct one sometimes and just keeps the wrong one.
***BUT, the extra one will disappear when the app is shut down and reloaded. So it's just in memory somewhere. I can verify in the store that everything is correct. But the NSFetchedResultsController isn't firing the controllerDidChange... stuff.
Here is the superclass of my view controller
CoreDataTableViewController.m
#pragma mark - Fetching
- (void)performFetch
{
self.debug = 1;
if (self.fetchedResultsController) {
if (self.fetchedResultsController.fetchRequest.predicate) {
if (self.debug) NSLog(#"[%# %#] fetching %# with predicate: %#", NSStringFromClass([self class]), NSStringFromSelector(_cmd), self.fetchedResultsController.fetchRequest.entityName, self.fetchedResultsController.fetchRequest.predicate);
} else {
if (self.debug) NSLog(#"[%# %#] fetching all %# (i.e., no predicate)", NSStringFromClass([self class]), NSStringFromSelector(_cmd), self.fetchedResultsController.fetchRequest.entityName);
}
NSError *error;
[self.fetchedResultsController performFetch:&error];
if (error) NSLog(#"[%# %#] %# (%#)", NSStringFromClass([self class]), NSStringFromSelector(_cmd), [error localizedDescription], [error localizedFailureReason]);
} else {
if (self.debug) NSLog(#"[%# %#] no NSFetchedResultsController (yet?)", NSStringFromClass([self class]), NSStringFromSelector(_cmd));
}
[self.tableView reloadData];
}
- (void)setFetchedResultsController:(NSFetchedResultsController *)newfrc
{
NSFetchedResultsController *oldfrc = _fetchedResultsController;
if (newfrc != oldfrc) {
_fetchedResultsController = newfrc;
newfrc.delegate = self;
if ((!self.title || [self.title isEqualToString:oldfrc.fetchRequest.entity.name]) && (!self.navigationController || !self.navigationItem.title)) {
self.title = newfrc.fetchRequest.entity.name;
}
if (newfrc) {
if (self.debug) NSLog(#"[%# %#] %#", NSStringFromClass([self class]), NSStringFromSelector(_cmd), oldfrc ? #"updated" : #"set");
[self performFetch];
} else {
if (self.debug) NSLog(#"[%# %#] reset to nil", NSStringFromClass([self class]), NSStringFromSelector(_cmd));
[self.tableView reloadData];
}
}
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (self.debug) NSLog(#"fetchedResultsController returns %d sections", [[self.fetchedResultsController sections] count]);
return [[self.fetchedResultsController sections] count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[[self.fetchedResultsController sections] objectAtIndex:section] numberOfObjects];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [[[self.fetchedResultsController sections] objectAtIndex:section] name];
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex: (NSInteger)index
{
return [self.fetchedResultsController sectionForSectionIndexTitle:title atIndex:index];
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return [self.fetchedResultsController sectionIndexTitles];
}
#pragma mark - NSFetchedResultsControllerDelegate
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
if (!self.suspendAutomaticTrackingOfChangesInManagedObjectContext)
{
[self.tableView beginUpdates];
self.beganUpdates = YES;
}
}
- (void)controller:(NSFetchedResultsController *)controller
didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
atIndex:(NSUInteger)sectionIndex
forChangeType:(NSFetchedResultsChangeType)type
{
if (!self.suspendAutomaticTrackingOfChangesInManagedObjectContext)
{
switch(type)
{
case NSFetchedResultsChangeInsert:
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
}
- (void)controller:(NSFetchedResultsController *)controller
didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath
forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath
{
if(self.debug) NSLog(#"controller didChangeObject: %#", anObject);
if (!self.suspendAutomaticTrackingOfChangesInManagedObjectContext)
{
NSLog(#"#########Controller did change type: %d", type);
switch(type)
{
case NSFetchedResultsChangeInsert:
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate:
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeMove:
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
if (self.beganUpdates) [self.tableView endUpdates];
if (self.debug) NSLog(#"controller Did Change Content");
}
- (void)endSuspensionOfUpdatesDueToContextChanges
{
_suspendAutomaticTrackingOfChangesInManagedObjectContext = NO;
}
- (void)setSuspendAutomaticTrackingOfChangesInManagedObjectContext:(BOOL)suspend
{
if (suspend) {
_suspendAutomaticTrackingOfChangesInManagedObjectContext = YES;
} else {
[self performSelector:#selector(endSuspensionOfUpdatesDueToContextChanges) withObject:0 afterDelay:0];
}
}
#end
And here's my specific view controller I subclassed from it:
- (NSArray *)sectionHeaderTitles
{
if (_sectionHeaderTitles == nil) _sectionHeaderTitles = [NSArray arrayWithObjects:#"Not Yet Uploaded", #"Uploaded But Not Featured", #"Previously Featured", nil];
return _sectionHeaderTitles;
}
- (NSDictionary *)selectedEntry
{
if (_selectedEntry == nil) _selectedEntry = [[NSDictionary alloc] init];
return _selectedEntry;
}
- (void)setupFetchedResultsController
{
[self.photoDatabase.managedObjectContext setStalenessInterval:0.0];
[self.photoDatabase.managedObjectContext.parentContext setStalenessInterval:0.0];
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:#"Animal"];
request.sortDescriptors = [NSArray arrayWithObjects:[NSSortDescriptor sortDescriptorWithKey:#"status" ascending:YES], [NSSortDescriptor sortDescriptorWithKey:#"unique" ascending:NO], nil];
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:self.photoDatabase.managedObjectContext sectionNameKeyPath:#"status" cacheName:nil];
NSError *error;
BOOL success = [self.fetchedResultsController performFetch:&error];
if (!success) NSLog(#"error: %#", error);
else [self.tableView reloadData];
self.fetchedResultsController.delegate = self;
}
- (void)useDocument
{
if (![[NSFileManager defaultManager] fileExistsAtPath:[self.photoDatabase.fileURL path]]) {
[self.photoDatabase saveToURL:self.photoDatabase.fileURL forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) {
[self setupFetchedResultsController];
}];
} else if (self.photoDatabase.documentState == UIDocumentStateClosed) {
[self.photoDatabase openWithCompletionHandler:^(BOOL success) {
[self setupFetchedResultsController];
}];
} else if (self.photoDatabase.documentState == UIDocumentStateNormal) {
[self setupFetchedResultsController];
}
}
- (void)setPhotoDatabase:(WLManagedDocument *)photoDatabase
{
if (_photoDatabase != photoDatabase) {
_photoDatabase = photoDatabase;
[self useDocument];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont fontWithName:#"AmericanTypewriter" size:20];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor whiteColor];
self.navigationItem.titleView = label;
label.text = self.navigationItem.title;
[label sizeToFit];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// Get CoreData database made if necessary
if (!self.photoDatabase) {
NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
url = [url URLByAppendingPathComponent:#"Default Photo Database"];
self.photoDatabase = [[WLManagedDocument alloc] initWithFileURL:url];
NSLog(#"No existing photoDatabase so a new one was created from default photo database file.");
}
self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"DarkWoodBackGround.png"]];
}
- (void)syncWithServer
{
// This is done on the syncQ
// Start the activity indicator on the nav bar
dispatch_async(dispatch_get_main_queue(), ^{
[self.spinner startAnimating];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.spinner];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(managedObjectContextDidSave:)
name:NSManagedObjectContextDidSaveNotification
object:self.photoDatabase.managedObjectContext.parentContext];
});
// Find new animals (status == 0)
NSFetchRequest *newAnimalsRequest = [NSFetchRequest fetchRequestWithEntityName:#"Animal"];
newAnimalsRequest.predicate = [NSPredicate predicateWithFormat:#"status == 0"];
NSError *error;
NSArray *newAnimalsArray = [self.photoDatabase.managedObjectContext.parentContext executeFetchRequest:newAnimalsRequest error:&error];
if ([newAnimalsArray count]) NSLog(#"There are %d animals that need to be uploaded.", [newAnimalsArray count]);
if (error) NSLog(#"fetchError: %#", error);
// Get the existing animals from the server
NSArray *parsedDownloadedAnimalsByPhoto = [self downloadedAllAnimalsFromWeb];
// In the parent context, insert downloaded animals into core data
for (NSDictionary *downloadedPhoto in parsedDownloadedAnimalsByPhoto) {
[Photo photoWithWebDataInfo:downloadedPhoto inManagedObjectContext:self.photoDatabase.managedObjectContext.parentContext];
// table will automatically update due to NSFetchedResultsController's observing of the NSMOC
}
// Upload the new animals if there are any
if ([newAnimalsArray count] > 0) {
NSLog(#"There are %d animals that need to be uploaded.", [newAnimalsArray count]);
for (Animal *animal in newAnimalsArray) {
// uploadAnimal returns a number that lets us know if it was accepted by the server
NSNumber *unique = [self uploadAnimal:animal];
if ([unique intValue] != 0) {
animal.unique = unique;
// uploadThePhotosOf returns a success BOOL if all 3 uploaded successfully
if ([self uploadThePhotosOf:animal]){
[self.photoDatabase.managedObjectContext performBlock:^{
animal.status = [NSNumber numberWithInt:1];
}];
}
}
}
}
[self.photoDatabase.managedObjectContext.parentContext save:&error];
if (error) NSLog(#"Saving parent context error: %#", error);
[self performUpdate];
// Turn the activity indicator off and replace the sync button
dispatch_async(dispatch_get_main_queue(), ^{
// Save the context
[self.photoDatabase saveToURL:self.photoDatabase.fileURL forSaveOperation:UIDocumentSaveForOverwriting completionHandler:^(BOOL success) {
if (success)
{
NSLog(#"Document was saved");
[self.photoDatabase.managedObjectContext processPendingChanges];
} else {
NSLog(#"Document was not saved");
}
}];
[self.spinner stopAnimating];
self.navigationItem.leftBarButtonItem = self.syncButton;
});
// Here it skips to the notification I got from saving the context so I can MERGE them
}
- (NSNumber *)uploadAnimal:(Animal *)animal
{
NSURL *uploadURL = [NSURL URLWithString:#"index.php" relativeToURL:self.remoteBaseURL];
NSString *jsonStringFromAnimalMetaDictionary = [animal.metaDictionary JSONRepresentation];
NSLog(#"JSONRepresentation of %#: %#", animal.namestring, jsonStringFromAnimalMetaDictionary);
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:uploadURL];
[request setPostValue:jsonStringFromAnimalMetaDictionary forKey:#"newmeta"];
[request startSynchronous];
NSError *error = [request error];
NSString *response;
if (!error) {
response = [request responseString];
NSNumber *animalUnique = [(NSArray *)[response JSONValue]objectAtIndex:0];
return animalUnique;
} else {
response = [error description];
NSLog(#"%# got an error: %#", animal.namestring, response);
return [NSNumber numberWithInt:0];
}
}
- (BOOL)uploadThePhotosOf:(Animal *)animal
{
NSURL *uploadURL = [NSURL URLWithString:#"index.php" relativeToURL:self.remoteBaseURL];
int index = [animal.photos count];
for (Photo *photo in animal.photos) {
// Name the jpeg file
NSTimeInterval timeInterval = [NSDate timeIntervalSinceReferenceDate];
NSString *imageServerPath = [NSString stringWithFormat:#"%lf-Photo.jpeg",timeInterval];
// Update the imageServerPath
photo.imageURL = imageServerPath;
NSData *photoData = [[NSData alloc] initWithData:photo.image];
NSString *photoMeta = [photo.metaDictionary JSONRepresentation];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:uploadURL];
[request addPostValue:photoMeta forKey:#"newphoto"];
[request addData:photoData withFileName:imageServerPath andContentType:#"image/jpeg" forKey:#"filename"];
[request setUploadProgressDelegate:self.progressView];
[request startSynchronous];
NSLog(#"%# progress: %#", animal.namestring, self.progressView.progress);
NSString *responseString = [request responseString];
NSLog(#"uploadThePhotosOf:%# photo at placement: %d has responseString: %#", animal.namestring, [photo.placement intValue], responseString);
SBJsonParser *parser= [[SBJsonParser alloc] init];
NSError *error = nil;
id jsonObject = [parser objectWithString:responseString error:&error];
NSNumber *parsedPhotoUploadResponse = [(NSArray *)jsonObject objectAtIndex:0];
// A proper response is not 0
if ([parsedPhotoUploadResponse intValue] != 0) {
photo.imageid = parsedPhotoUploadResponse;
--index;
}
}
// If the index spun down to 0 then it was successful
int success = (index == 0) ? 1 : 0;
return success;
}
- (NSArray *)downloadedAllAnimalsFromWeb
{
NSURL *downloadURL = [NSURL URLWithString:#"index.php" relativeToURL:self.remoteBaseURL];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:downloadURL];
[request setPostValue:#"yes" forKey:#"all"];
request.tag = kGetHistoryRequest;
[request startSynchronous];
NSString *responseString = [request responseString];
NSLog(#"downloadedAllAnimalsFromWeb responseString: %#", responseString);
SBJsonParser *parser= [[SBJsonParser alloc] init];
NSError *error = nil;
id jsonObject = [parser objectWithString:responseString error:&error];
NSArray *parsedDownloadedResponseStringArray = [NSArray arrayWithArray:jsonObject];
return parsedDownloadedResponseStringArray;
}
- (void)performUpdate
{
NSManagedObjectContext * context = self.photoDatabase.managedObjectContext.parentContext;
NSSet * inserts = [context updatedObjects];
if ([inserts count])
{
NSError * error = nil;
NSLog(#"There were inserts");
if ([context obtainPermanentIDsForObjects:[inserts allObjects]
error:&error] == NO)
{
NSLog(#"BAM! %#", error);
}
}
[self.photoDatabase updateChangeCount:UIDocumentChangeDone];
}
- (void)managedObjectContextDidSave:(NSNotification *)notification
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSManagedObjectContextDidSaveNotification object:self.photoDatabase.managedObjectContext.parentContext];
NSLog(#"userInfo from the notification: %#", [notification userInfo]);
// Main thread context
NSManagedObjectContext *context = self.fetchedResultsController.managedObjectContext;
SEL selector = #selector(mergeChangesFromContextDidSaveNotification:);
[context performSelectorOnMainThread:selector withObject:notification waitUntilDone:YES];
NSLog(#"ContextDidSaveNotification was sent. MERGED");
}
#pragma mark - Table view data source
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"EntryCell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"EntryCell"];
}
// Configure the cell here...
Animal *animal = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = animal.namestring;
if (([animal.numberofanimals intValue] > 0) && animal.species) {
cell.detailTextLabel.text = [NSString stringWithFormat:#"%#s", animal.species];
} else {
cell.detailTextLabel.text = animal.species;
}
return cell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
Animal *animal = [self.fetchedResultsController objectAtIndexPath:indexPath];
// be somewhat generic here (slightly advanced usage)
// we'll segue to ANY view controller that has a photographer #property
if ([segue.identifier isEqualToString:#"newAnimal"]) {
NSLog(#"self.photodatabase");
[(NewMetaEntryViewController *)[segue.destinationViewController topViewController] setPhotoDatabaseContext:self.photoDatabase.managedObjectContext];
} else if ([segue.destinationViewController respondsToSelector:#selector(setAnimal:)]) {
// use performSelector:withObject: to send without compiler checking
// (which is acceptable here because we used introspection to be sure this is okay)
[segue.destinationViewController performSelector:#selector(setAnimal:) withObject:animal];
NSLog(#"animal: %# \r\n indexPath: %#", animal, indexPath);
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 30;
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return nil;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
NSLog(#"header for section called for section: %d", section);
NSLog(#"fetchedResultsController sections: %#", self.fetchedResultsController.sections);
CGRect headerRect = CGRectMake(0, 0, tableView.bounds.size.width, 30);
UIView *header = [[UIView alloc] initWithFrame:headerRect];
UILabel *headerTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, tableView.bounds.size.width - 10, 20)];
if ([(Animal *)[[[[self.fetchedResultsController sections] objectAtIndex:section] objects] objectAtIndex:0] status] == [NSNumber numberWithInt:0]) {
headerTitleLabel.text = [self.sectionHeaderTitles objectAtIndex:0];
} else if ([(Animal *)[[[[self.fetchedResultsController sections] objectAtIndex:section] objects] objectAtIndex:0] status] == [NSNumber numberWithInt:1]) {
headerTitleLabel.text = [self.sectionHeaderTitles objectAtIndex:1];
} else {
headerTitleLabel.text = [self.sectionHeaderTitles objectAtIndex:2];
}
headerTitleLabel.textColor = [UIColor whiteColor];
headerTitleLabel.font = [UIFont fontWithName:#"AmericanTypewriter" size:20];
headerTitleLabel.backgroundColor = [UIColor clearColor];
headerTitleLabel.alpha = 0.8;
[header addSubview:headerTitleLabel];
return header;
}
Way too much code for anyone to want to wade through.
However, from a quick inspection, it looks like you are violating the MOC constraints. Specifically, you are accessing the parent context directly, and not from its own thread, either.
Typically, you would start a new thread, then create a MOC in that thread, make its parent be the MOC of the document. then do your stuff, and call save on the new MOC. It will then notify the parent, which should handle the updating.
I am having problem in managing UITableView. I 'm posting a screenshot here to briefly explain the problem. Image 1 shows the default View when First Time view Appears. When I tap on yellow button(Yellow Button is in a custom table section header view) I open an UIAlertView with table in it like shown in the image 2. Then selecting any option I insert a custom cell with a button,textfield and another button. I have a mutable Array for each section so when I select an option I add that string into the corresponding section array and reloadtable.See image 3. Now when I enter values in UITextfields the values replaced by another cells. See the two images below for understanding the problem. image 4. Also when I delete a cell and insert a new the textfield is preloaded with previous value.
And here is the .m file implementation code for analyze the problem
#import "contactsViewController.h"
#import "textFieldCell.h"
#import "SBTableAlert.h"
#import "PhoneFieldCell.h"
#import "DHValidation.h"
#define kTextFieldTag 222
#define kTitleButtonTag 111
#define MAX_LENGTH 20
#define charecterLimit 13
#define PHONE_NUMBER #"0123456789+-"
#implementation contactsViewController
#synthesize currentTextField;
#synthesize choiceList;
#synthesize labelHeaders;
#synthesize selectedIndexPath;
#synthesize aTable;
#synthesize customText;
#synthesize currentSelectionType;
#synthesize phoneList;
#synthesize emailList;
#synthesize otherDetailsList;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
//Prepare array's for display in table header and in custom alert option table
[self prepareDataForTable];
[self initializeProperties];
//Set selected index to -1 by default
selectedIndex = -1;
}
//prepare array's for table
- (void) prepareDataForTable {
NSArray *temp = [[NSArray alloc] initWithObjects:#"work",#"home",#"mobile",#"custom",nil];
self.choiceList = temp;
[temp release];
NSArray *temp1 = [[NSArray alloc] initWithObjects:#"First Name",#"Last Name",nil];
self.labelHeaders = temp1;
[temp1 release];
}
- (void) initializeProperties {
//Initialize Mutable array's
NSMutableArray *temp = [[NSMutableArray alloc] initWithCapacity:0];
self.phoneList = temp;
[temp release];
NSMutableArray *temp1 = [[NSMutableArray alloc] initWithCapacity:0];
self.emailList = temp1;
[temp1 release];
NSMutableArray *temp2 = [[NSMutableArray alloc] initWithCapacity:0];
self.otherDetailsList = temp2;
[temp2 release];
}
#pragma mark -
- (IBAction) btnCancelTapped:(id) sender {
[self dismissModalViewControllerAnimated:YES];
}
- (IBAction) btnDoneTapped:(id) sender {
[self.currentTextField resignFirstResponder];
//TODO: Fetch all the data from all the cells and notify the delegate.
NSMutableDictionary *allSectionsData = [[[NSMutableDictionary alloc] initWithCapacity:0] autorelease];
NSMutableDictionary *section1Dictionary = [[[NSMutableDictionary alloc] initWithCapacity:0] autorelease];
NSMutableDictionary *phoneSectionDictionary = [[[NSMutableDictionary alloc] initWithCapacity:0] autorelease];
NSMutableDictionary *emailSectionDictionary = [[[NSMutableDictionary alloc] initWithCapacity:0] autorelease];
NSMutableDictionary *otherSectionDictionary = [[[NSMutableDictionary alloc] initWithCapacity:0] autorelease];
//For Section 0
for (unsigned rowIndex = 0; rowIndex < 2; rowIndex++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:rowIndex inSection:0];
UITableViewCell *cell = nil;
cell = (textFieldCell*)[aTable cellForRowAtIndexPath:indexPath];
UITextField *txtf = (UITextField *)[cell viewWithTag:kTextFieldTag];
//TextField validation
DHValidation *validation = [[DHValidation alloc] init];
NSString *str = [txtf.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSString *alertMessage = nil;
BOOL isvalid = NO;
if(![validation validateNotEmpty:str])
alertMessage = #"NameField should not be Empty";
else
isvalid = TRUE;
if(!isvalid){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:alertMessage delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alert show];
[alert release];
return ;
}
[validation release];
NSString *type = nil;
NSString *value = nil;
if(rowIndex == 0)
type = #"First Name";
else if(rowIndex == 1){
type = #"Last Name";
}
value = txtf.text;
if(!value){
//Do not insert that value in the dictinary
value = #"";
}else {
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:type,#"type",value,#"value",nil];
[section1Dictionary setObject:dictionary forKey:[NSNumber numberWithInt:rowIndex]];
}
}
if([section1Dictionary count] > 0) {
[allSectionsData setObject:section1Dictionary forKey:#"PersonalDetailsSection"];
}
//For Section 1
for (unsigned rowIndex = 0; rowIndex < [phoneList count]; rowIndex++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:rowIndex inSection:1];
UITableViewCell *cell = nil;
cell = (PhoneFieldCell*)[aTable cellForRowAtIndexPath:indexPath];
UITextField *txtf = (UITextField *)[cell viewWithTag:kTextFieldTag];
UIButton *btnTitle = (UIButton*)[cell viewWithTag:kTitleButtonTag];
NSString *type = nil;
NSString *value = nil;
type = [btnTitle currentTitle];
value = [txtf text];
if(!value || [[value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] isEqualToString:#""] ){
//Do not insert that value in the dictinary
continue;
}else {
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:type,#"type",value,#"value",nil];
[phoneSectionDictionary setObject:dictionary forKey:[NSNumber numberWithInt:rowIndex]];
//[phoneSectionDictionary setObject:value forKey:type];
}
}
if([phoneSectionDictionary count] > 0) {
[allSectionsData setObject:phoneSectionDictionary forKey:#"PhoneSection"];
}
//For Section 2
for (unsigned rowIndex = 0; rowIndex < [emailList count]; rowIndex++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:rowIndex inSection:2];
UITableViewCell *cell = nil;
cell = (PhoneFieldCell*)[aTable cellForRowAtIndexPath:indexPath];
UITextField *txtf = (UITextField *)[cell viewWithTag:kTextFieldTag];
UIButton *btnTitle = (UIButton*)[cell viewWithTag:kTitleButtonTag];
NSString *type = nil;
NSString *value = nil;
type = [btnTitle currentTitle];
value = [txtf text];
if(!value || [[value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] isEqualToString:#""] ){
//Do not insert that value in the dictinary
continue;
}else {
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:type,#"type",value,#"value",nil];
[emailSectionDictionary setObject:dictionary forKey:[NSNumber numberWithInt:rowIndex]];
}
}
if([emailSectionDictionary count] > 0) {
[allSectionsData setObject:emailSectionDictionary forKey:#"EmailSection"];
}
//for Section 3
for (unsigned rowIndex = 0; rowIndex < [phoneList count]; rowIndex++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:rowIndex inSection:3];
UITableViewCell *cell = nil;
cell = (PhoneFieldCell*)[aTable cellForRowAtIndexPath:indexPath];
UITextField *txtf = (UITextField *)[cell viewWithTag:kTextFieldTag];
UIButton *btnTitle = (UIButton*)[cell viewWithTag:kTitleButtonTag];
NSString *type = nil;
NSString *value = nil;
type = [btnTitle currentTitle];
value = [txtf text];
if(!value || [[value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] isEqualToString:#""] ){
//Do not insert that value in the dictinary
continue;
}else {
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:type,#"type",value,#"value",nil];
[otherSectionDictionary setObject:dictionary forKey:[NSNumber numberWithInt:rowIndex]];
//[phoneSectionDictionary setObject:value forKey:type];
}
}
if([otherSectionDictionary count] > 0) {
[allSectionsData setObject:otherSectionDictionary forKey:#"OtherSection"];
}
}
#pragma mark -
#pragma mark UITableView Data Source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 4;
}
// Returns the number of rows in a given section.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSInteger count = 0;
switch (section) {
case 0:
count = 2;
break;
case 1://Phone
count = [phoneList count];
break;
case 2://Email
count = [emailList count];
break;
case 3://Other
count = [otherDetailsList count];
break;
default:
count = 0;
break;
}
return count;
}
// Returns the cell for a given indexPath.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"textFieldCustomCell";
static NSString *PhoneFieldCustomCellIdentifier = #"PhoneFieldCustomCell";
static NSString *EmailFieldCustomCellIdentifier = #"EmailFieldCustomCell";
static NSString *OtherDetailsCustomCellIdentifier = #"OtherDetailsCustomCell";
UITableViewCell *cell = nil;
switch (indexPath.section) {
case 0:{
cell = (textFieldCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"textFieldCell" owner:self options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[textFieldCell class]]){
cell = (textFieldCell *) currentObject;
break;
}
}
}
}
break;
case 1:{
cell = (PhoneFieldCell *)[tableView dequeueReusableCellWithIdentifier:PhoneFieldCustomCellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"PhoneFieldCell" owner:self options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[PhoneFieldCell class]]){
cell = (PhoneFieldCell *) currentObject;
((PhoneFieldCell *)cell).enterText.delegate = self;
((PhoneFieldCell *)cell).enterText.text = nil;
break;
}
}
}
}
break;
case 2:{
cell = (EmailFieldCell *)[tableView dequeueReusableCellWithIdentifier:EmailFieldCustomCellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"EmailFieldCell" owner:self options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[EmailFieldCell class]]){
cell = (EmailFieldCell *) currentObject;
((EmailFieldCell *)cell).enterText.delegate = self;
((EmailFieldCell *)cell).enterText.text = nil;
break;
}
}
}
}
break;
case 3:{
cell = (OtherDetailsCell *)[tableView dequeueReusableCellWithIdentifier:OtherDetailsCustomCellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"OtherDetailsCell" owner:self options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[OtherDetailsCell class]]){
cell = (OtherDetailsCell *) currentObject;
((OtherDetailsCell *)cell).enterText.delegate = self;
((OtherDetailsCell *)cell).enterText.text = nil;
break;
}
}
}
}
break;
default:
break;
}
//Setup cell data
switch (indexPath.section) {
case 0:{
((textFieldCell*)cell).aTextField.delegate = self;
if(indexPath.row == 0){
((textFieldCell*)cell).aTextField.placeholder = #"Enter First Name";
}
if(indexPath.row == 1){
((textFieldCell*)cell).aTextField.placeholder = #"Enter Last Name";
}
((textFieldCell*)cell).aLabel.text = [self.labelHeaders objectAtIndex:indexPath.row];
}
break;
case 1:{
NSString *str = [phoneList objectAtIndex:indexPath.row];
[((PhoneFieldCell *)cell).changeBtn setTitle:str forState:UIControlStateNormal];
[((PhoneFieldCell *)cell).changeBtn addTarget:self action:#selector(changeButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
((PhoneFieldCell *)cell).btnDeleteCell.tag = indexPath.row;
[((PhoneFieldCell *)cell).btnDeleteCell addTarget:self action:#selector(deleteButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
}
break;
case 2:{
NSString *str = [emailList objectAtIndex:indexPath.row];
[((EmailFieldCell *)cell).changeBtn setTitle:str forState:UIControlStateNormal];
[((EmailFieldCell *)cell).changeBtn addTarget:self action:#selector(changeButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
((EmailFieldCell *)cell).btnDeleteCell.tag = indexPath.row;
[((EmailFieldCell *)cell).btnDeleteCell addTarget:self action:#selector(deleteButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
}
break;
case 3:{
NSString *str = [otherDetailsList objectAtIndex:indexPath.row];
[((OtherDetailsCell *)cell).changeBtn setTitle:str forState:UIControlStateNormal];
[((OtherDetailsCell *)cell).changeBtn addTarget:self action:#selector(changeButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
((OtherDetailsCell *)cell).btnDeleteCell.tag = indexPath.row;
[((OtherDetailsCell *)cell).btnDeleteCell addTarget:self action:#selector(deleteButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
}
break;
default:
break;
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 30;
}
- (UIView*) tableView: (UITableView*) tableView viewForHeaderInSection: (NSInteger) section {
if (section == 0) {
return nil;
}
return [self headerViewForSection:section];
}
// Handle row selection
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}
- (UIView*) headerViewForSection:(NSInteger)section {
CGRect lblTitleFrame = CGRectMake(15, 0, 200, 20);
CGRect btnFrame = CGRectMake(280.0, 0.0, 30.0, 30.0);
CGRect headerViewFrame = CGRectMake(0,0, 40, 30);
NSString *lblTitleText = nil;
switch (section) {
case 1://phone
lblTitleText = #"Phone";
break;
case 2://email
lblTitleText = #"Email";
break;
case 3://other details
lblTitleText = #"Other";
break;
default:
break;
}
//Create a header view with a label and a button
UIView *headerView = [[[UIView alloc] initWithFrame:headerViewFrame] autorelease];
UILabel *titleForTable = [[UILabel alloc]initWithFrame:lblTitleFrame];
titleForTable.text = lblTitleText;
titleForTable.backgroundColor = [UIColor clearColor];
titleForTable.textColor = [UIColor whiteColor];
titleForTable.shadowColor = [UIColor whiteColor];
[headerView addSubview:titleForTable];
[titleForTable release];
UIButton *phoneButton = [[UIButton alloc] initWithFrame:btnFrame];
phoneButton.alpha = 0.7;
phoneButton.tag = section;
[phoneButton setImage:[UIImage imageNamed:#"Yellow.png"] forState: UIControlStateNormal];
[phoneButton addTarget: self action: #selector(headerTapped:) forControlEvents: UIControlEventTouchUpInside];
[headerView addSubview: phoneButton];
[phoneButton release];
return headerView;
}
#pragma mark -
#pragma mark UITextField Delegate
- (void)textFieldDidBeginEditing:(UITextField *)textField {
self.currentTextField = textField;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
//textfield charecters range
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField.text.length >= MAX_LENGTH && range.length == 0){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Warning" message:#"You reached maximum limit - 20"
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alert show];
[alert release];
return NO; // return NO to not change text
}
else {
return YES;
}
}
#pragma mark -
- (void)deleteButtonTapped:(id)sender{
if([self.currentTextField isFirstResponder]) {
[self.currentTextField resignFirstResponder];
}
NSLog(#"Button Pressed");
UIButton *btnDelete = (UIButton*)sender;
id cell = [[btnDelete superview] superview];
if([cell isKindOfClass:[PhoneFieldCell class]]) {
cell = (PhoneFieldCell*)cell;
}
if([cell isKindOfClass:[EmailFieldCell class]]) {
cell = (EmailFieldCell*)cell;
}
if([cell isKindOfClass:[OtherDetailsCell class]]) {
cell = (OtherDetailsCell*)cell;
}
NSIndexPath *indexPath = [aTable indexPathForCell:cell];
NSLog(#"Section is %d and row is %d",indexPath.section,indexPath.row);
switch (indexPath.section) {
case 1:
[self.phoneList removeObjectAtIndex:[btnDelete tag]];
[aTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
break;
case 2:
[self.emailList removeObjectAtIndex:[btnDelete tag]];
[aTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
break;
case 3:
[self.otherDetailsList removeObjectAtIndex:[btnDelete tag]];
[aTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
break;
default:
break;
}
[aTable reloadData];
}
- (void)changeButtonTapped:(id)sender{
if([self.currentTextField isFirstResponder]) {
[self.currentTextField resignFirstResponder];
}
UIButton *btnDelete = (UIButton*)sender;
id cell = (PhoneFieldCell*)[[btnDelete superview] superview];
if([cell isKindOfClass:[PhoneFieldCell class]]) {
cell = (PhoneFieldCell*)cell;
}
if([cell isKindOfClass:[EmailFieldCell class]]) {
cell = (EmailFieldCell*)cell;
}
if([cell isKindOfClass:[OtherDetailsCell class]]) {
cell = (OtherDetailsCell*)cell;
}
self.selectedIndexPath = [aTable indexPathForCell:cell];
shouldModify = YES;
NSLog(#"Section is %d and row is %d",self.selectedIndexPath.section,self.selectedIndexPath.row);
SBTableAlert *alert = nil;
alert = [[[SBTableAlert alloc] initWithTitle:#"Options" cancelButtonTitle:#"Cancel" messageFormat:#"Select your option!"] autorelease];
[alert setType:SBTableAlertTypeSingleSelect];
[alert.view addButtonWithTitle:#"OK"];
[alert setDelegate:self];
[alert setDataSource:self];
[alert show];
}
- (void)headerTapped:(id)sender {
if([self.currentTextField isFirstResponder]) {
[self.currentTextField resignFirstResponder];
}
UIButton *tappedButton = (UIButton*)sender;
//set current selection according to section
switch ([tappedButton tag]) {
case 1://Phone
self.currentSelectionType = SELECTIONTYPE_PHONE;
break;
case 2://Email
self.currentSelectionType = SELECTIONTYPE_EMAIL;
break;
case 3://Other details
self.currentSelectionType = SELECTIONTYPE_OTHER;
break;
default:
break;
}
SBTableAlert *alert;
alert = [[[SBTableAlert alloc] initWithTitle:#"Options" cancelButtonTitle:#"Cancel" messageFormat:#"Select your option!"] autorelease];
[alert setType:SBTableAlertTypeSingleSelect];
[alert.view addButtonWithTitle:#"OK"];
[alert setDelegate:self];
[alert setDataSource:self];
[alert show];
}
#pragma mark - SBTableAlertDataSource
- (UITableViewCell *)tableAlert:(SBTableAlert *)tableAlert cellForRow:(NSInteger)row {
UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil] autorelease];
[cell.textLabel setText:[self.choiceList objectAtIndex:row]];
if(row == selectedIndex)
cell.accessoryType = UITableViewCellAccessoryCheckmark;
else {
cell.accessoryType == UITableViewCellAccessoryNone;
}
return cell;
}
- (NSInteger)numberOfRowsInTableAlert:(SBTableAlert *)tableAlert {
if (tableAlert.type == SBTableAlertTypeSingleSelect)
return [self.choiceList count];
else
return 4;
}
#pragma mark - SBTableAlertDelegate
- (void)tableAlert:(SBTableAlert *)tableAlert didSelectRow:(NSInteger)row {
if (tableAlert.type == SBTableAlertTypeMultipleSelct) {
UITableViewCell *cell = [tableAlert.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:0]];
if (cell.accessoryType == UITableViewCellAccessoryNone)
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
else
[cell setAccessoryType:UITableViewCellAccessoryNone];
[tableAlert.tableView deselectRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:0] animated:YES];
}
else if (tableAlert.type == SBTableAlertTypeSingleSelect) {
selectedIndex = row;
[tableAlert.tableView reloadData];
[tableAlert.tableView deselectRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:0] animated:YES];
}
}
- (void)tableAlert:(SBTableAlert *)tableAlert didDismissWithButtonIndex:(NSInteger)buttonIndex {
if(buttonIndex == 1){
if(selectedIndex == -1){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Select at least one choice" message:nil
delegate:nil
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alert show];
[alert release];
}
else if(cellSelected == FALSE){
if(selectedIndex == 3){
UIAlertView *customAlert = [[UIAlertView alloc] initWithTitle:#"Enter Custom Message" message:#"\n\n"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Ok",nil];
customAlert.tag = 99;
CGAffineTransform myTransForm = CGAffineTransformMakeTranslation(0,0);
UITextField *temp = [[UITextField alloc] initWithFrame:CGRectMake(15, 50, 255, 30)];
self.customText = temp;
[temp release];
self.customText.backgroundColor = [UIColor whiteColor];
self.customText.placeholder = #"Enter Custom Text";
self.customText.clearButtonMode = UITextFieldViewModeWhileEditing;
self.customText.layer.cornerRadius = 5;
[customAlert addSubview:self.customText];
[customAlert setTransform:myTransForm];
[customAlert show];
[customAlert release];
}else {
UITableViewCell *cell = [tableAlert.tableView cellForRowAtIndexPath:[ NSIndexPath indexPathForRow:selectedIndex inSection:0]];
NSString *val = cell.textLabel.text;
if(!shouldModify) {
switch (self.currentSelectionType) {
case SELECTIONTYPE_PHONE:
[phoneList addObject:val];
break;
case SELECTIONTYPE_EMAIL:
[emailList addObject:val];
break;
case SELECTIONTYPE_OTHER:
[otherDetailsList addObject:val];
break;
default:
break;
}
}
else {
switch (self.selectedIndexPath.section) {
case 1:
[phoneList replaceObjectAtIndex:self.selectedIndexPath.row withObject:val];
break;
case 2:
[emailList replaceObjectAtIndex:self.selectedIndexPath.row withObject:val];
break;
case 3:
[otherDetailsList replaceObjectAtIndex:self.selectedIndexPath.row withObject:val];
break;
default:
break;
}
shouldModify = NO;
}
}
}
if(self.currentSelectionType != SELECTIONTYPE_UNKNOWN || self.selectedIndexPath.section > 0)
[aTable reloadData];
selectedIndex = -1;
[tableAlert release];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if(alertView.tag == 99 && buttonIndex == 1){
NSString *val = [self.customText text];
if(!val || [[val stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] isEqualToString:#""]){
//show error alert here and return from here
UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"Please fill a value for custom text"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[errorAlert show];
[errorAlert release];
return;
}
switch (self.currentSelectionType) {
case SELECTIONTYPE_PHONE:
[phoneList addObject:val];
[aTable reloadData];
break;
case SELECTIONTYPE_EMAIL:
[emailList addObject:val];
[aTable reloadData];
break;
case SELECTIONTYPE_OTHER:
[otherDetailsList addObject:val];
[aTable reloadData];
break;
default:
break;
}
}
}
#pragma mark -
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
self.currentTextField = nil;
self.choiceList = nil;
self.labelHeaders = nil;
self.selectedIndexPath = nil;
self.aTable = nil;
self.customText = nil;
self.phoneList = nil;
self.emailList = nil;
self.otherDetailsList = nil;
self.customText = nil;
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[phoneList release];
[emailList release];
[otherDetailsList release];
[customText release];
[customText release];
[aTable release];
[selectedIndexPath release];
[choiceList release];
[labelHeaders release];
[currentTextField release];
[super dealloc];
}
#end
here is your answer http://www.icodeblog.com/2011/01/04/elctextfieldcell-a-useful-tableviewcell-for-forms/
u can also manually do some customization according to your app need
- (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView reloadData];
}
Hi when I am compiling my code i am getting this error-"accessing unknown 'flipDelegate' component of a property"
this is the code from where i am getting error-
//
// RootViewController.m
// Dolphia
//
// Created by Dolphia Nandi on 4/3/11.
// Copyright 2011 State University of New York at Buffalo. All rights reserved.
//
#import "RootViewController.h"
#import "MainViewController.h"
#import "FlipSideViewController.h"
#implementation RootViewController
#synthesize mainViewController;
#synthesize flipSideViewController;
- (void)loadMainViewController {
MainViewController *viewController = [[MainViewController alloc] initWithNibName:#"MainViewController" bundle:nil];
self.mainViewController = viewController;
self.mainViewController.flipDelegate = self;
[viewController release];
}
- (void)loadFlipSideViewController {
FlipSideViewController *viewController = [[FlipSideViewController alloc] initWithNibName:#"FlipSideViewController" bundle:nil];
self.flipSideViewController = viewController;
self.flipSideViewController.flipDelegate = self;
[viewController release];
}
- (void)viewDidLoad {
[self loadMainViewController]; // Don't load the flipside view unless / until necessary
[self.view addSubview:mainViewController.view];
}
// This method is called when either of the subviews send a delegate message to us.
// It flips the displayed view from the whoever sent the message to the other.
- (void)toggleView:(id)sender {
if (flipSideViewController == nil) {
[self loadFlipSideViewController];
}
UIView *mainWindow = mainViewController.view;
UIView *flipSideView = flipSideViewController.view;
/*[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
if(flipeffect >= 0 && flipeffect < 2) {
flipeffect++;
[UIView setAnimationTransition:((voiceViewController == sender) ? UIViewAnimationTransitionFlipFromRight : UIViewAnimationTransitionFlipFromLeft) forView:self.view cache:YES];
} else if (flipeffect >= 2 && flipeffect < 4) {
flipeffect++;
[UIView setAnimationTransition:((voiceViewController == sender) ? UIViewAnimationTransitionCurlUp : UIViewAnimationTransitionCurlDown) forView:self.view cache:YES];
} else if (flipeffect >= 4 && flipeffect < 6) {
flipeffect++;
[UIView setAnimationTransition:((voiceViewController == sender) ? UIViewAnimationTransitionFlipFromLeft : UIViewAnimationTransitionFlipFromRight) forView:self.view cache:YES];
} else {
flipeffect++;
if(flipeffect > 7)
flipeffect = 0;
[UIView setAnimationTransition:((voiceViewController == sender) ? UIViewAnimationTransitionCurlDown : UIViewAnimationTransitionCurlUp) forView:self.view cache:YES];
}*/
if (mainViewController == sender) {
[flipSideViewController viewWillAppear:YES];
[mainViewController viewWillDisappear:YES];
[mainWindow removeFromSuperview];
[self.view addSubview:flipSideView];
[mainViewController viewDidDisappear:YES];
[flipSideViewController viewDidAppear:YES];
} else {
[mainViewController viewWillAppear:YES];
[flipSideViewController viewWillDisappear:YES];
[flipSideView removeFromSuperview];
[self.view addSubview:mainWindow];
[flipSideViewController viewDidDisappear:YES];
[mainViewController viewDidAppear:YES];
}
//[UIView commitAnimations];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}
- (void)dealloc {
[mainViewController release];
[flipSideViewController release];
[super dealloc];
}
#end
Both mainViewController and flipSideViewController should have flipDelegate as instance variable and make sure you add #synthesize and #property
I want to add simple search bar in the table view. Program getting run with no error, but when I try to add text in search bar, program gets terminates
following is my code:
#interface RootViewController : UITableViewController<UISearchBarDelegate> {
NSArray *list;
IBOutlet UISearchBar *searchBar;
NSMutableArray *searchresult;
BOOL isSearchon;
BOOL canSelectRow;
}
#property(nonatomic, retain)NSArray *list;
#property(nonatomic, retain)IBOutlet UISearchBar *searchBar;
-(void)doneSearching:(id)sender;
-(void)searchlist;
#end
//in .m
#implementation RootViewController
#synthesize list,searchBar;
-(void)doneSearching:(id)sender
{
isSearchon=NO;
canSelectRow=YES;
self.tableView.scrollEnabled=YES;
self.navigationItem.rightBarButtonItem=nil;
[searchBar resignFirstResponder];
[self.tableView reloadData];
}
-(void)searchlist
{NSString *searchText = searchBar.text;
[searchresult removeAllObjects];
for(NSString *str in list)
{
NSRange titleResultsRange=[str rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (titleResultsRange.length > 0)
[searchresult addObject:str];
}
}
- (void) searchBarSearchButtonClicked:(UISearchBar *)theSearchBar {
[self searchlist];
}
- (void)searchBar:(UISearchBar *)theSearchBar textDidChange:(NSString *)searchText {
if([searchText length]>0)
{isSearchon =YES;
canSelectRow=YES;
self.tableView.scrollEnabled=YES;
[self searchlist];}
else{
isSearchon =NO;
canSelectRow=NO;
self.tableView.scrollEnabled=NO;
}
[self.tableView reloadData];
}
- (void)viewDidLoad {
[super viewDidLoad];
list = [[NSArray alloc]initWithObjects:#"rohan",#"vidhya",#"kavita",#"pushkar",nil];
self.tableView.tableHeaderView = searchBar;
searchBar.autocorrectionType=UITextAutocorrectionTypeYes;
searchresult=[[NSMutableArray alloc]init];
isSearchon =NO;
canSelectRow=YES;
self.navigationItem.title = #"Names";
}
-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
isSearchon =YES;
canSelectRow=NO;
self.tableView.scrollEnabled=NO;
self.navigationItem.rightBarButtonItem=[[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneSearching:)]autorelease];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(isSearchon){
return [searchresult count];}
else{
return [list count];}
}
// Customize the appearance of table view cells.
- (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] autorelease];
}
if(isSearchon)
{
cell.text=[searchresult objectAtIndex:indexPath.row];}
else{
cell.text=[list objectAtIndex:indexPath.row];}
// Configure the cell.
return cell;
}
- (void)dealloc {
[super dealloc];
[searchBar release];
}
#end
I have edited my code in my question...it works fine nw....AnyOne willing to use searchbar in tableview in Iphones can refer the code.