get value of selected row NSTableView - objective-c

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

Related

passing Data from TableView after searchResult

(Using X-Code 4.6)
I have a problem with my TableView:
I have like a 100 Names in there.
Click on one and it goes to the detailView with buttons and Labels.
Everything works fine until i use the Searchbar.
Of course after in the search Results the name that was at row 85 is ow at row 1 and it passes the data from row 1.
Is it possible to somehow permanently attach all data (strings) from row 85 together?
I have 4 Strings: Name, CellPhoneNumber, BuisNumber and Mail.
Name goes to a label and the rest are variables to buttons.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"toDetail"]) {
DetailViewController *destViewController = segue.destinationViewController;
NSIndexPath *indexPath = nil;
if ([self.searchDisplayController isActive]) {
indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
destViewController.nameIdentity = [searchResults objectAtIndex:indexPath.row];
}
else {
indexPath = [self.tableView indexPathForSelectedRow];
destViewController.nameIdentity = [nameData objectAtIndex:indexPath.row];
destViewController.cellNumberIdentity = [cellNumberData objectAtIndex:indexPath.row];
destViewController.buisNumberIdentity = [buisNumberData objectAtIndex:indexPath.row];
destViewController.mailIdentity = [mailData objectAtIndex:indexPath.row];
} }
}
I do get the correct Name to the label, but how to get the rest.
If I understood you right it will be better to create class e.g. Person. it will have 4 parameters name, mail, cellNumber, businessNumber.
Then You will need 2 arrays fullPersonData and currentSearchPersonData.
So that next time when you will doing search you will need:
1)to search in fullPerson array occurrences of search word and copy them to currentSearchData
2)reload table with currentSearchData array (you can use bool flag like isSearch to trig tableView delegates to take data from needed array)
After that there is no need to think on what row was cell before search just toke objectAtIndex from neded array and pass it to detailView. and detailView will fill itself))

UITableView Cells with Formatted Text Parsed from an XML

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 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! ;-)

get value from custom UITableViewCell

I have a custom UITableViewCell that has a UISegmentedControl object. My UITableView (questionnaire form) has 6 of this custom cell (6 segmented controls). I am having trouble getting the selectedSegmentIndex of the segment controls. My guess is that the cells are being released after the table is generated. I am getting the values using the following code:
MyCustomCell *q1 = (MyCustomCell *)[tableView cellForRowAtIndexPath:[NSIndexPath indexPathWithIndex:0]];
int segmentIndex = (int) q1.segmentedController.selectedSegmentIndex;
the int segmentIndex is always giving the same value of 0 no matter what the selected index is.
You must use the right method to initialize your index path for the section and row you want use, otherwise row and section properties won't be correctly set, and you will get the same cell each time :
+(NSIndexPath *)indexPathForRow:(NSUInteger)row inSection:(NSUInteger)section;.
The code looks like :
// getting the cell at third row of first section
NSIndexPath *ip = [NSIndexPath indexPathForRow:2 inSection:0];
MyCustomCell *q1 = (MyCustomCell *)[tableView cellForRowAtIndexPath:ip];
int segmentIndex = (int) q1.segmentedController.selectedSegmentIndex;
You could also consult : NSIndexPath UIKit Additions Reference for more information.

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"?