UITableView Cells with Formatted Text Parsed from an XML - objective-c

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"];

Related

indexOfObjectIdenticalTo: returns value outside of array count

I'm currently working on an app that populates a UITableView with items from a MPMediaItemCollection. I'm trying to add a UITableViewCellAccessoryCheckmark to the row that matches the title of the currently playing track.
I've done so by creating a mutable array of the track titles, which are also set for my cell's textLabel.text property. (for comparison purposes)
Note: This is all done in - (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath
MPMediaItem *mediaItem = (MPMediaItem *)[collectionMutableCopy objectAtIndex: row];
if (mediaItem) {
cell.textLabel.text = [mediaItem valueForProperty:MPMediaItemPropertyTitle];
}
[mutArray insertObject:cell.textLabel.text atIndex:indexPath.row];
To the best of my knowledge this all works fine except for the below. At this point, I am trying to get the index of the currently playing tracks title and add the UITableViewCellAccessoryCheckmark to that row.
if (indexPath.row == [mutArray indexOfObjectIdenticalTo:[mainViewController.musicPlayer.nowPlayingItem valueForProperty:MPMediaItemPropertyTitle]]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
Getting to my question, I added all of the above (mostly irrelevant) code because I'm stumped on where I went wrong. When I log indexOfObjectIdenticalTo: it spits out "2147483647" every time, even though there are never more than 5 objects in the array. But why?
If anyone has any tips or pointers to help me fix this it would be greatly appreciated!
2147483647 just mean the object is not found.
From the documentation of -[NSArray indexOfObjectIdenticalTo:]:
Return Value
The lowest index whose corresponding array value is identical to anObject. If none of the objects in the array is identical to anObject, returns NSNotFound.
and NSNotFound is defined as:
enum {
NSNotFound = NSIntegerMax
};
and 2147483647 = 0x7fffffff is the maximum integer on 32-bit iOS.
Please note that even if two NSString have the same content, they may not be the identical object. Two objects are identical if they share the same location, e.g.
NSString* a = #"foo";
NSString* b = a;
NSString* c = [a copy];
assert([a isEqual:b]); // a and b are equal.
assert([a isEqual:c]); // a and c are equal.
assert(a == b); // a and b are identical.
assert(a != c); // a and c are *not* identical.
I believe you just want equality test instead of identity test, i.e.
if (indexPath.row == [mutArray indexOfObject:[....]]) {
Looking at the docs for NSArray
Return Value
The lowest index whose corresponding array value is identical to anObject. If none of the objects in the array is identical to anObject, returns NSNotFound.
So you should probably do a check
NSInteger index = [array indexOfObjectIdenticalTo:otherObject];
if (NSNotFound == index) {
// ... handle not being in array
} else {
// ... do your normal stuff
}

how to access the labels using tags in objective-c

I am implementing an iphone application.Which is a UItableview.It has two sections.Three labels are added to all the cells in section-I.I have given tags 1,2,3 for those 3 labels when adding to the cells.Now I want to get the values from label3 from all the cells from section-I and I would like to add all the float values and display the total in another label which is in section-II.I read that I should give different tags for different cell's label3(label with tag-3).How is it possible please guide me.I have float values in the label3.I wanna add all the values and present the result in section-II.Please help me with some reference.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString* curItem = [self.finalItems objectAtIndex:indexPath.row];
NSString* curQuantity = [self.finalPrices objectAtIndex:indexPath.row];
NSString* priceStr = [self fetchDishRecordswithpredicate:curItem];
NSMutableString* priceValStr = [NSMutableString stringWithString:priceStr];
[priceValStr replaceCharactersInRange:NSMakeRange(0, 1) withString:#""];
float totalPrice = [curQuantity floatValue]*[priceValStr floatValue];
UILabel *lblTemp1 = (UILabel *)[cell viewWithTag:1];
UILabel *lblTemp2 = (UILabel *)[cell viewWithTag:2];
UILabel *lblTemp3 = (UILabel *)[cell viewWithTag:3];
switch (indexPath.section) {
case 0: {
lblTemp1.text = curItem;
lblTemp2.text = curQuantity;
lblTemp3.text = [NSString stringWithFormat:#"$ %0.2f", price];
} break;
case 1: {
lblTemp1.text = #"Your Total:";
lblTemp3.text = #"How can I get Total here";
} break;
}
return cell;
}
It should never be necessary to get the value of a label, whether it's part of a table cell or some other kind of view. The user can't modify a label -- they're for display only. That means that the only way that a label can get a given value is for your program to set that value, and that in turn means that you've already got the data you need somewhere. That holds true for any kind of view other than controls that are used for getting input from the user.
The fact that you're looking to get values from labels probably means that you're using your views to store data, which is not a good practice. Views display data, but that data should be stored elsewhere in the program, typically in a data model (assuming you're following the MVC paradigm).
All that said, you can get the cell for a given row from your table using its -cellForRowAtIndexPath: method. From there, you can access the cells subviews using the usual -viewWithTag: method. You definitely don't need a unique set of tags for each cell -- you just need to get the cell for the row that you're interested in. However, keep in mind that a table usually only keeps the cells that are visible around; if you ask it for the cell for a row that's not visible, it'll probably have to create that cell, which in turn will involve asking the table's delegate (your own code!) for the cell. That's a pretty expensive way to ask yourself for data that you already have! ;-)

fetching data from tableview cell?

God evening :-D
i have a a tableview,(using core data) i pub the cell with a "Todo" like: "5 mile Run" and i set the detailed text to #"points value #", X were x is a number set by a slider, and the same time u set name for the Todo.
i have put in a button in the cell, and called it add, and i want to be able to add that number in the detailed text to a "totalPoints" attribute in my core data model.
i can make a fetchRequest for the entity,but how do i make sure that i get that number, and how do i use simple math when the "pointValue" is stored in NSNumber object.
Update :
fixed it :-D
if u add a button to your cell, u can get that indexPath like this :
- (IBAction)buttonTapped:(id)sender {
if (![sender isKindOfClass: [UIButton class]]) return;
UITableViewCell *cell = (UITableViewCell *)[sender superview];
if (![cell isKindOfClass: [UITableViewCell class]]) return;
NSIndexPath *indexPath = [self.tableView indexPathForCell: cell];
// do something with indexPath.row and/or indexPath.section.
this fixed my app, and it´s now in beta testing :-P
Thanks for your help
Skov
In your sample code, there are a couple issues. First, when you are getting the values a, b and e you can just do this:
NSNumber *a = toDo.pointsValue;
(assuming that toDo.pointsValue is an NSNumber).
Secondly, in your if statement you are comparing the values of the pointers, not the values in the objects that the pointers point to.
This code should have the result you want (I assume you initialize toDo in some way) :
ToDo *toDo;
CGFloat x = [toDo.pointsValue floatValue] + [toDo.totalPoint floatValue];
//Note: it is better to use CGFloat rather than plain float
CGFloat y = [toDo.goodiesPoints floatValue];
if (x >= y)
{
[self performSelector:#selector(winView)
withObject:nil
afterDelay:0.5];
}
I am not sure exactly what you mean when you say "how do i make sure that i get that number". What number is "that number"?

Tableview cells does not display data when in integer format

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];

get value of selected row NSTableView

how can i get value of selected row in NSTableView?
By using selectedRow, see here:
#property(readonly) NSInteger selectedRow;
The index of the last selected row (or the last row added to the
selection).
You may also be interested in:
–selectedRowEnumerator (deprecated)
–numberOfSelectedRows
–selectedRowIndexes "returns an index set containing the indexes of the selected rows"
Cocoa follows the Model-View-Controller design pattern, so your reasoning as well as your own designs should as well.
You don't get any values from the table because you already have them in your model. Your controller (usually the data source) asks the table for its selected row index(es), then asks your model for the objects matching those indexes, then does whatever it needs to do.
Assuming that you already have property named tableView and in xib you defined tableView with row cellName
NSInteger row = [tableView selectedRow];
NSTableColumn *column = [tableView tableColumnWithIdentifier:#"cellName"];
NSCell *cell = [column dataCellForRow:row];
NSLog(#"cell value:%#", [cell stringValue]);
To get selected row from table try this..
[tableView selectedRow];
To access the tableView value
[array objectAtIndex:[tableView selectedRow]];
In Swift2.0, this could help you:
func tableViewSelectionDidChange(notification: NSNotification) {
let selectedTableView = notification.object as! NSTableView
print(selectedTableView.selectedRow)
}
As we have all the value in our model, so we can only get the index of row and get the value using index. for e.g:
NSInteger row = [self.nsTableView selectedRow]; //here #property (weak) IBOutlet NSTableView *nsTableView;
NSString *cellValue = ObjectValue[row];// NSMutableArray *ObjectValue;
NSLog(#"%#", cellValue);