How to prevent all rows of an NSTableView being selected at startup - objective-c

I am new to Objective-C. I am using NSArrayController to fill NSTableView. Initially, I am getting all rows selected in the table view. I am unable to find the reason behind it.
for(int i=0;i<nCount;i++)
{
NSString *fileName_File = [[[filenames objectAtIndex:i] lastPathComponent] stringByDeletingPathExtension];
NSString *pathExtension = [[filenames objectAtIndex:i] pathExtension];
NSString *yourPath = [filenames objectAtIndex:i];
NSFileManager *fmgr = [[NSFileManager alloc] init];
NSDictionary *attrs = [fmgr attributesOfItemAtPath: yourPath error: nil];
[attrs retain];
UInt32 result = [attrs fileSize];
/*NSString * zStr1 = [[NSString alloc]initWithFormat:#"%d",i+1];
NSString * zStr2 = [[NSString alloc]initWithFormat:#"%#",fileName_File];
NSString * zStr3 = [[NSString alloc]initWithFormat:#"%#",pathExtension];
NSString * zStr4 = [[NSString alloc]initWithFormat:#"%d",result];
NSString * zStr5 = [[NSString alloc]initWithFormat:#"%#",[filenames objectAtIndex:i]];*/
CMyMediaData * MyMediaObj = [[CMyMediaData alloc]initWithString1:[[NSString alloc]initWithFormat:#"%#",#""]
andString2:[[NSString alloc]initWithFormat:#"%#",fileName_File]
andString3:[[[NSString alloc]initWithFormat:#"%#",pathExtension]uppercaseString]
andString4:[[NSString alloc]initWithFormat:#"%d",result]
andString5:[[NSString alloc]initWithFormat:#"%#",[filenames objectAtIndex:i]]
];
[attrs release];
[mMedia.mcMediaController.mcTableViewMyMedia addObject:MyMediaObj];
//[mMedia.nsMutaryOfDataObject addObject:MyMediaObj];
} this is the code to add data to nsarraycontroller object.mcMediaController is object of NSArrayController

NSArrayController has a property selectsInsertedObjects. If that is YES, inserted objects are marked selected, including the ones added by addObject:.
You can set that property to NO, or you can use the following to unset the selection after adding the items:
mMedia.mcMediaController.mcTableViewMyMedia.selectedObjects = #[];

Related

Reading from SQL database into NSArray

I have an iPad that reads data from an SQL database. The following code works fine and retrieves 2 fields from each record and reads them into an NSArray.
I now need to read 5 of the fields and I can't help but think that there is a better way of doing it rather than running 5 separate queries through php (the getinfo.php file with the choice parameter set to pick the different fields).
Any pointers to a better method for doing this?
NSString *strURLClass = [NSString stringWithFormat:#"%#%#", #"http://wwwaddress/getinfo.php?choice=1&schoolname=",obsSchoolName];
NSArray *observationsArrayClass = [[NSMutableArray alloc] initWithContentsOfURL:[NSURL URLWithString:strURLClass]];
observationListFromSQL = [[NSMutableArray alloc]init];
NSEnumerator *enumForObsClass = [observationsArrayClass objectEnumerator];
NSString *strURLDate = [NSString stringWithFormat:#"%#%#", #"http://wwwaddress/getinfo.php?choice=5&schoolname=",obsSchoolName];
NSArray *observationsArrayDate = [[NSMutableArray alloc] initWithContentsOfURL:[NSURL URLWithString:strURLDate]];
observationListFromSQL = [[NSMutableArray alloc]init];
NSEnumerator *enumForObsDate = [observationsArrayDate objectEnumerator];
id className, dateOfObs;
while (className = [enumForObsClass nextObject])
{
dateOfObs = [enumForObsDate nextObject];
[observationListFromSQL addObject:[NSDictionary dictionaryWithObjectsAndKeys:className, #"obsClass", dateOfObs, #"obsDate",nil]];
}
Yes, you can do that with less code by "folding" the statements into a loop, and using a mutable dictionary:
// Add other items that you wish to retrieve to the two arrays below:
NSArray *keys = #[#"obsClass", #"obsDate"]; // Key in the dictionary
NSArray *choices = #[#1, #5]; // Choice in the URL string
NSMutableArray *res = [NSMutableArray array];
NSMutableArray *observationListFromSQL = [NSMutableArray array];
for (int i = 0 ; i != keys.count ; i++) {
NSNumber *choice = choices[i];
NSString *strURLClass = [NSString stringWithFormat:#"http://wwwaddress/getinfo.php?choice=%#&schoolname=%#", choice, obsSchoolName];
NSArray *observationsArray = [[NSMutableArray alloc] initWithContentsOfURL:[NSURL URLWithString:strURLClass]];
NSEnumerator *objEnum = [observationsArrayClass objectEnumerator];
NSString *key = keys[i];
NSMutableDictionary *dict;
if (res.count < i) {
dict = res[i];
} else {
dict = [NSMutableDictionary dictionary];
[res addObject:dict];
}
id item;
while (item = [objEnum nextObject]) {
[res setObject:item forKey:key];
}
}

How to parse 5 Arrays in one object using JSON

I'm stuck at this problem, The output of the JSOn is
"forecast":[
{"day":"Today","condition":"MostlyClear","high_temperature":"94","low_temperature":"70"},
{"day":"Tomorrow","condition":"Sunny","high_temperature":"95","low_temperature":"69"},
{"day":"Saturday","condition":"Sunny","high_temperature":"94","low_temperature":"71"},
{"day":"Sunday","condition":"Sunny","high_temperature":"93","low_temperature":"70"},
{"day":"Monday","condition":"Sunny","high_temperature":"94","low_temperature":"70"}
]
I need condition key to show the values in the tableview cell. And my code is
temp=[[NSString alloc] initWithData:[dataLoader httpData] encoding:NSUTF8StringEncoding];
NSLog(#"Patient Details %#",temp);
array = [[temp JSONValue] objectForKey:#"forecast"];
NSDictionary* dic = [array objectAtIndex:[indexPath row]];
NSString *strDay = [dic objectForKey:#"day"];
NSString *stringCondition = [dic objectForKey:#"condition"];
NSLog(#"condition is %#", stringCondition);
NSString *stringTemp = [dic objectForKey:#"high_temperature"];
NSString *stringLow = [dic objectForKey:#"low_temperature"];
But it is showing only one condition value, i want all the five conditions at a time. I also tried to use NSArray instead of NSString for condtion with no luck.
array = [[temp JSONValue] objectForKey:#"forecast"];
Make a for loop in this place :
for(NSUInteger index = 0; index<[array count]; index++)
{
NSDictionary* dic = [array objectAtIndex:index];
NSString *strDay = [dic objectForKey:#"day"];
NSString *stringCondition = [dic objectForKey:#"condition"];
NSLog(#"condition is %#", stringCondition);
NSString *stringTemp = [dic objectForKey:#"high_temperature"];
NSString *stringLow = [dic objectForKey:#"low_temperature"];
}
Hope this will help...

Adding Annotations for coordinates MKMapView

I am parsing this JSON file (correctly, it works with the UITextFields):
{"longitude":["37.786793","39.388528"],"latitude":["-122.395416","-78.887734"]}
ind creating MapViews, with the respective annotations in this way:
NSArray *allKeys2 = [DictionaryMap allKeys];
for (int h = 0; h < [allKeys2 count]; h++) {
CGRect mapFrame = CGRectMake( 400, e, 200, 110);
MKMapView *mapView2 = [[MKMapView alloc] initWithFrame:mapFrame];
[image1 addSubview:mapView2];
NSString *key2 = [allKeys2 objectAtIndex:i];
NSObject *obj2 = [DictionaryMap objectForKey:key2];
NSString *address = [NSString stringWithFormat:#"%#", obj2];
float stringFloat = [address floatValue];
float stringFloat2 = [key2 floatValue];
CLLocationCoordinate2D anyLocation;
anyLocation.longitude = stringFloat;
anyLocation.latitude = stringFloat2;
MKPointAnnotation *annotationPoint2 = [[MKPointAnnotation alloc] init]; annotationPoint2.coordinate = anyLocation;
annotationPoint2.title = #"Event";
annotationPoint2.subtitle = #"Microsoft's headquarters2";
[mapView2 addAnnotation:annotationPoint2];
[mapView2.userLocation setTitle:#"I am here"];
[mapView2.userLocation addObserver:self
forKeyPath:#"location"
options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld)
context:NULL];
[mapView2 setShowsUserLocation:NO];
[MapViewArray addObject:mapView2];
if (MapViewArray == nil)MapViewArray = [[NSMutableArray alloc]init];
[MapViewArray addObject: mapView2];
}}
}while(g < f);
...I want the first map view to show the first coordinate pin, and the second to show the second pair of coordinates. But now, it is plotting on all the map views the same pin, corresponding to the last coordinates, and not to the first and second... respectively. This method works for the UITextField text, so I can't find the problem.
Please help!!
EDIT:
NSString *filenameMap = [NSString stringWithFormat:#"%#%#Map", destDir, NavBar.topItem.title];
NSString *MapPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#Map", NavBar.topItem.title]];
[self.restClient loadFile:filenameMap intoPath:MapPath];
NSString *fileContentMap = [[NSString alloc] initWithContentsOfFile:MapPath];
SBJsonParser *parserMap = [[SBJsonParser alloc] init];
NSDictionary *dataMap = (NSDictionary *) [parserMap objectWithString:fileContentMap error:nil];
NSArray *MaparrayLongitude = [dataMap objectForKey:#"longitude"];
NSArray *MaparrayLatitude = [dataMap objectForKey:#"latitude"];
NSDictionary* DictionaryMap = [NSDictionary dictionaryWithObjects:MaparrayLatitude forKeys:MaparrayLongitude];
Another take:
You probably don't want to map your latitudes to longitudes in DictionaryMap. How about making an NSArray of NSDictionaries, where each dictionary has a "latitude" key and a "longitude" key?
Also, you have "[MapViewArray addObject: mapView2]" twice in your code.
And what is "g"? Maybe add some logging to your loop so you can see what's being created?

opencv ios cvseq storage

I am intending to pre-load all the images that I have stored inside application. Pre-loading of images involves:
Read images from bundle.
Extract object descriptors using cvExtractSurf from opencv framework.
Store IPLImage with corresponding object descriptors and keypoints.
I am having an issue in creating a dictionary containing CvSeq* keys and CvSeq* descs.
Please suggest how to store these values in NSMutableDictionary.
-(void) preloadImages:(NSMutableDictionary *)dictionary{
NSArray *d = [[NSBundle mainBundle] pathsForResourcesOfType:#"png" inDirectory:nil];
CvSURFParams params = cvSURFParams(500, 1);
CvMemStorage* storage = cvCreateMemStorage(0);
for( int i=0;i<[d count];i++){
NSString *searchForMe = #"myapp.app/1";
NSString *s = [[NSString alloc] initWithString:[d objectAtIndex:i]];
NSRange range = [s rangeOfString:searchForMe];
if( range.location != NSNotFound ){
NSMutableDictionary *surfDict = [[NSMutableDictionary alloc] init];
NSString *substring = [s substringFromIndex:range.location];
substring = [substring stringByReplacingOccurrencesOfString:#"myapp.app/" withString:#""];
UIImage *testImage = [UIImage imageNamed:substring];
IplImage *iplTestImage = [OpenCVUtilities CreateGRAYIplImageFromUIImage:testImage];
CvSeq *keys = 0 ;
CvSeq *descs = 0;
cvExtractSURF( iplTestImage, 0, &keys, &descs, storage, params );
[surfDict setObject:(id)testImage forKey:#"uiImage"];
NSLog(#"Image name : %#", substring);
[dictionary setObject:surfDict forKey:[NSString stringWithFormat:#"%d",i]];
[dictionary setObject:(NSObject *)keys forKey:#"keys"]; // error here
[dictionary setObject:(NSObject *)descs forKey:#"descs"]; // error here
[surfDict release];
}
}
}
Create a class that has an instance variable of type cvseq, add your cvseq to the object, and add that class to the dictionary.

Cryptic SQLite console output in Objective-C

When running my program to query the iPod library of my iPhone, I get the following output in the console:
CPSqliteStatementPerform: attempt to write a readonly database for UPDATE ddd.ext_container SET orig_date_modified = (SELECT date_modified FROM container WHERE pid=container_pid) WHERE orig_date_modified=0
I have disabled everything that outputs to the console so it isn't as if I am having an error with NSLog. What is going on here and how can I fix it. I'll include the overall class source code that I am calling, along with the class that does the work (sorry for posting it all:
+(NSMutableDictionary *) makeQuery {
MPMediaQuery *query = [[MPMediaQuery alloc] init]; //query iPod library
NSString *facebookIDKey = #"Facebook ID";
NSString *genericFacebookID = #"1234567890";
NSMutableDictionary *organizedSongData = [NSMutableDictionary dictionaryWithObject:genericFacebookID forKey:facebookIDKey];
NSArray *allSongs = [query collections];
//only add those songs which have not
//been played since last login
for (MPMediaItem *recent in allSongs) {
NSDate *lastPlayed = [recent valueForProperty:MPMediaItemPropertyLastPlayedDate];
int songCounter = 1;
BOOL uploadInfo = [[PlayedSinceLastLogin alloc] initWithLastPlayedDateOfSong:lastPlayed];
if (uploadInfo == YES) {
//adds songs into
[organizedSongData addEntriesFromDictionary: [MakeDicForSongInfo initWithMPMediaItem:recent andSongNumber:songCounter]];
songCounter++;
}
}
return organizedSongData;
}
Here is the actual brunt of the work, done in the MakeDicForSongInfo:
+(NSDictionary *) initWithMPMediaItem:(MPMediaItem *) song andSongNumber: (int)songCount{
//Create & initialize NSStrings for keys
//Create NSArray to hold keys
NSString *songKey = [NSString stringWithFormat: #"%i Song Title", songCount];
NSString *albumKey = [NSString stringWithFormat: #"%i Album", songCount];
NSString *artistKey = [NSString stringWithFormat: #"%i Artist", songCount];
NSString *artistIDKey = [NSString stringWithFormat: #"%i Artist Persistent ID", songCount];
NSString *lastPlayedKey = [NSString stringWithFormat: #"%i Late Played Date", songCount];
NSString *genreKey = [NSString stringWithFormat: #"%i Genre", songCount];
NSString *ratingKey = [NSString stringWithFormat: #"%i User Rating", songCount];
NSString *playCountKey = [NSString stringWithFormat: #"%i Play Count", songCount];
NSArray *propertyKeys = [[NSArray alloc] initWithObjects:songKey, albumKey, artistKey, artistIDKey,
lastPlayedKey, genreKey, ratingKey, playCountKey, nil];
//Create & initialize NSStrings to hold song property information
//Create NSArray to hold the values
NSString *songTitle = [song valueForProperty:MPMediaItemPropertyTitle];
NSString *albumName = [song valueForProperty:MPMediaItemPropertyAlbumTitle];
NSString *artistName = [song valueForProperty:MPMediaItemPropertyArtist];
NSString *artistID = [song valueForProperty:MPMediaItemPropertyArtistPersistentID];
NSString *lastPlayed = [song valueForProperty:MPMediaItemPropertyLastPlayedDate];
NSString *genre = [song valueForProperty:MPMediaItemPropertyGenre];
NSString *userRating = [song valueForProperty:MPMediaItemPropertyRating];
NSString *playCount = [song valueForProperty:MPMediaItemPropertyPlayCount];
NSArray *propertyValues = [[NSArray alloc] initWithObjects:songTitle, albumName, artistName, artistID,
lastPlayed, genre, userRating, playCount, nil];
//Create NSDictionary to store information, initializing it with
//above data, then returning the resulting dictionary
NSDictionary *songInfo = [[NSDictionary alloc] initWithObjects:propertyValues forKeys:propertyKeys];
return songInfo;
}
Could it be something in accessing all that Media information that spooks the iPhone?
Apparently this is a bug in iOS 4.3 and is addressed in this devforum at Apple:
https://devforums.apple.com/message/428584#428584