My application has a sqlite database and i m using executeQuery methods to retrive and insert data.
Problem
I cannot assign integer values cells of tableview. It does not display data which is in integer format. i want to store integer values into a tableview and show sorted integer
data to user.
Here i am posting my code
1) in viewDidLoad i have retrieved integer values from database table "dummy" and taken it into an NSArray "Scores"
2) i have assigned a value of "highscore" which is an integer type to cell in "cellForRowAtIndexPath" Method of table view.Note that converting this data into NSString does not allow sorting.
But this shows no data in table cells when i run it.
1)
NSString *myDBnew = #"/Users/taxsmart/Documents/sqlite/atest.sql";
database = [[Sqlite alloc] init];
[database open:myDBnew];
Scores = [database executeQuery:#"SELECT * FROM dummy ORDER BY highscore DESC;"];
2)
cell.textLabel.text = [[Scores objectAtIndex:indexPath.row] valueForKey:#"highscore"];
What is Wrong?
Please Give Your Suggestions. Your Suggestions are most welcome.
You must assign an NSString* to cell.textLabel.text. Just embedd your ints into an NSString.
NSInteger highscore = [[Scores objectAtIndex:indexPath.row] valueForKey:#"highscore"];
cell.textLabel.text = [NSString stringWithFormat:#"%d", highscore];
Related
ran into a simple objc problem and need some help. I'm sending a query to a server that gives me back a tuple wrapped in jSON. Just using the dictionary to sort everything out.
- (void)viewDidLoad
{
[super viewDidLoad];
Nodes *node = [Nodes new];
NSDictionary *jsonDict = [node nodeActivityforNode:#"17" withDates:nil and:nil];
NSDictionary *jsonActivityDict = [jsonDict objectForKey:#"activity"];
NSLog(#"activity?: %#", jsonActivityDict);
}
and then heres the debug dump
activity?:
(
(
"2013-05-21 16:58:32",
0
),
(
"2013-05-21 16:58:15",
0
),
(
"2013-05-21 16:57:59",
0
),
I'm trying to split up the comma separated values so I can put them in individual int and string objects but just cant think of how to get in there. Any help would be appreciated. Thanks!
The "activity" data looks like an array of arrays, not a dictionary. If you know you will always get three pairs of values and you want those 6 values put into 6 separate variables then you could do this:
NSDictionary *jsonDict = [node nodeActivityforNode:#"17" withDates:nil and:nil];
NSArray *jsonActivities = [jsonDict objectForKey:#"activity"];
NSString *date1 = jsonActivities[0][0];
int int1 = [jsonActivities[0][1] intValue];
NSString *date2 = jsonActivities[1][0];
int int2 = [jsonActivities[1][1] intValue];
NSString *date3 = jsonActivities[2][0];
int int3 = [jsonActivities[2][1] intValue];
Note that this code will crash if there are less than 3 sets of pairs or if any of those three "pairs" only has one value instead of two.
I am Parsing an XML file from a URL using an NSXMLParser, the items that I parse are added to an array that a UITableView loads its data from. Among other things I am parsing titles for events and dates that those events take place on. These items are parsed and added to an array called stories. My goal is to have the title of an event be the main text for each cell in my TableView and the date be a subtitle. The title of each event does not need to be formatted (i.e. if the XML file reads: "Event One" then that is what should be displayed) I am successful with this using this method:
(title being the title of an item in the XML, stories being the array that the TableView loads its data from.)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:MyIdentifier];
}
// Set up the cell
NSUInteger storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
cell.textLabel.text = [[stories objectAtIndex: storyIndex] objectForKey: #"title"];
return cell;
}
However I do need to format all of the dates I parse, I do so with an NSDateFormatter, I am satisfied with the format of the dates after the NSDateFormatter has done its job. If i did not need to format the dates I could use:
cell.detailTextLabel.text = [[stories objectAtIndex: storyIndex] objectForKey: #"isodate"];
to achieve my goal. Once the NSDateFormatter has done its job I have a variable (a NSMutableString) called currentDate with the string value for my formatted dates, using the console I can see that this yields the desired result for every item in the XML. When I use:
cell.textLabel.text = currentDate;
I end up with every cell in my table having the date of the final item in the XML file (granted it looks nice is formatted correctly). I have tried moving my methods around to no avail. If you wouldn't mind pointing me in the right direction, I need to know how to include the value of my variable currentDate in the subtitle text of a UITableView in a way in which the title of an item and the date of the item in a cell correspond with one another.
A couple of thoughts:
Regarding your date problem, it sounds like you're storing the date in a single instance variable called currentDate. And given that you didn't share that code, I gather you're not doing that in cellForRowAtIndexPath, but rather, perhaps in your parser, or something like that. Unfortunately, that would give every cell in the table the date for the last row you parsed). You should either
call your date formatting right in cellForRowAtIndexPath (or, better, call a method that does your date formatting), specifically grab [[stories objectAtIndex: storyIndex] objectForKey: #"isodate"], format it, and set detailTextLabel.text accordingly; or
rather than storing the formatted date in a single currentDate variable, add an dictionary key for the formatted date and store it in the mutable dictionary with everything else you parsed from the XML (e.g. read in isodate, format a string and save it back to the same dictionary with a unique key, maybe formattedDate).
NSUInteger storyIndex = indexPath.row; is a more common syntax if you're grabbing the news item associated with the given row of the tableview.
You can store your parse data into an NSDictionary with Key - value pair. Here you can use title as key and date as value and by this way both data will be corresponding to each one.
The problem is line
NSUInteger storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
you should be using indexPath.row instead,
cell.textLabel.text = [[stories objectAtIndex: indexPath.row] objectForKey: #"title"];
How do I update a single value in each row of my array?
For example I have an array created from an SQL DB with the following columns.
Name
Address
Lat
Long
Distance
Distance is initially set to 0 until calculated from the Lat/Long. I want to update that 0 with the calculated distance.
Every example I've found doesn't show how to update a single column per row.
NSMutableArray *barInfo = [BarDatabase database].barInfo;
for (Bars *info in barInfo) {
NSLog(#"%#, %#, %#, %#, %#", info.barName, info.barAddress, info.Lat, info.Long, info.cachedDist);
NSString *strLat = info.Lat;
NSString *strLong = info.Long;
CLLocation *barLocation = [[CLLocation alloc] initWithLatitude:[strLat doubleValue] longitude:[strLong doubleValue]];
CLLocationDistance bdistance = [currentLat distanceFromLocation:barLocation]/1000;
[barInfo addObject:bdistance];
}
I'm using an NSMutableArray because I can't figure out how to modify the Distance field in the NSArray. I know Apple states this is possible, but I can't find it.
how to assign an integer data type array values to UITextField
because i have an integer array and i want to display its contents in the textfield
try this,
NSString *intergerString = [NSString StringWithFormat:#"%d",intergerValue];
txtField.text = intergerString;
[*yourtextfieldname* setText:[NSString stringWithFormat:#"%d",yourintegervalue]];
I've tried NSMutableDictionary however I don't seem to be able to get an object by index.
Any ideas?
EDIT:
I've trying to create a uitableview sections object, which will store the header titles and be able to increment a counter for the rows. I need to be able to get the counter by index, counter value by title value.
Simplest way is to use 2 collections: dictionary for section infos (row numbers, countries etc) and array for section titles.
NSMutableDictionary *sectionInfos;
NSMutableArray *sectionTitles;
When you need a section info by sectionTitle:
NSDictionary *info = [sectionInfos objectForKey:sectionTitle];
int rowsCount = ((NSArray *)[info objectForKey:#"Countries"]).count;
When you need a section info by sectionIndex:
NSString *title = [sectionTitles objectAtIndex:sectionIndex];
NSDictionary *info = [sectionInfos objectForKey:title];
int rowsCount = ((NSArray *)[info objectForKey:#"Countries"]).count;
When you add a section, add sections info:
[sectionInfos setObject:info forKey:sectionTitle];
and a title to array, so infos and titles will be in sync.
[sectionTitles addObject:sectionTitle];
UPDATE: if the only info needed for section is number of rows:
UPDATE2: added types.
NSMutableDictionary *sectionRowCounts;
NSMutableArray *sectionTitles;
Rows count by sectionTitle:
int rowCount = [[sectionRowCounts objectForKey:sectionTitle] intValue];
Rows count by sectionIndex:
NSString *title = [sectionTitles objectAtIndex:sectionIndex];
int rowCount = [[sectionRowCounts objectForKey:title] intValue];
Adding a section:
[sectionRowCounts setObject:[NSNumber numberWithInt:rowCount] forKey:sectionTitle];
[sectionTitles addObject:sectionTitle];
Dictionaries are not ordered; therefore the objects in them do not have an index.
http://developer.apple.com/library/ios/#documentation/cocoa/reference/foundation/Classes/NSDictionary_Class/Reference/Reference.html#//apple_ref/occ/cl/NSDictionary
You need to use the key to retrieve a particular object from a dictionary. If you need to have the objects in a specific order, then you would probably use NSArray instead.
UPDATE
In your edit, you don't show what tableSectionArray is, but it looks like it's a dictionary (which makes it poorly named). You should use an NSArray, not an NSDictionary, to store what you want. If you need more than one value to be stored, then store an object that contains the values you need. Create a class that has the required values as properties; or, if appropriate, add NSDictionary objects to your array. (Based on how you are trying to assign an element from tableSectionArray, it looks like you do want it to contain dictionaries.) But you need the tableSectionArray itself to be an NSArray.
Yes, keep trying with NSMutableDictionary. It's the data structure you need for that. Can you post your code to see why it's not returning the value you expect?
Example:
NSString *yourvalue = #"Hello!";
NSMutableDictionary *d;
[d setValue:yourvalue forKey:#"yourkey"];
NSString *retrievedvalue = [d valueForKey:#"yourkey"];
// you should get value here