(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))
Related
After selecting a row, pressing TAB will normally move focus to the next editable text field(s), or in my case, column. However, I have noticed that only sometimes after pressing TAB, the row will turn gray and focus is lost. I must then click on the text field to continue. After days of observations, I still cannot detect a repeatable pattern, and internet searches have not given me enough information. I wonder if my delegate might be the culprit. Some questions come to mind.
Can anyone shed light on why or how focus might be lost? (indirectly answered by Willeke's comment to remove some code)
Is there a proper or deliberate way to force focus on the next text field? (now irrelevant)
The unpredictable behavior prompting the first two questions seemed to be interference between two methods. Resolving one has at least led to predictable behavior and this related question. I included more code below with another method. One column contains a pop-up button. Ordinarily, choosing a value does not select the row (ie, highlighted). I would like the row to be selected and focused (ie, highlighted blue) so that I can immediately TAB to the next field and begin editing. In editPopUps:, selectRowIndexes: will select the row, but the row is gray instead of blue and hence TAB has no effect. How do I select and focus the row?
Update: Here are some pertinent methods. I setup each view based on column because only some columns are editable.
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
NSString *column = tableColumn.identifier;
NSTableCellView *value = [tableView makeViewWithIdentifier:column owner:self];
Foo *foo = self.foos[row];
[self setupView:value column:column foo:foo];
[self populateCellView:value column:column foo:foo object:[foo valueForKey:column]];
return value;
}
- (void)setupCellView:(NSTableCellView *)view column:(NSString *)column foo:(Foo *)foo {
view.textField.alignment = NSRightTextAlignment;
BOOL editable = YES;
if ([attribute isEqualToString:column] || ![foo.x isEqualToNumber:bar.x]) {
editable = NO;
} else {
view.textField.target = self;
view.textField.action = #selector(editTextField:);
}
[view.textField setEditable:editable];
}
- (void)editTextField:(NSTextField *)sender {
if ([self.fooTableView numberOfSelectedRows]) {
NSTableCellView *cellView = (NSTableCellView *)[sender superview];
NSString *column = cellView.identifier;
NSInteger row = [self.fooTableView rowForView:cellView];
Foo *foo = self.foos[row];
NSNumber *amountNumber = [NSNumber numberWithShort:[sender intValue]];
if ([attribute isEqualToString:column]) {
foo.y = amountNumber;
} else {
foo.z = amountNumber;
}
[self.fooTableView reloadDataForRowIndexes:[NSIndexSet indexSetWithIndex:row] columnIndexes:[NSIndexSet indexSetWithIndex:[self.fooTableView columnWithIdentifier:column]]];
[self refreshSelection];
}
}
- (void)editPopUps:(NSPopUpButton *)sender {
GlorpView *glorpView = (GlorpView *)[sender superview];
NSString *column = pokeCellView.identifier;
NSInteger row = [self.fooTableView rowForView:glorpView];
Foo *foo = self.foos[row];
NSNumber *indexValue = [NSNumber numberWithShort:[sender indexOfSelectedItem]];
NSMutableIndexSet *columnIndexes = [NSMutableIndexSet indexSetWithIndex:[self.fooTableView columnWithIdentifier:column]];
if ([attribute isEqualToString:column]) {
foo.Z = indexValue;
}
[self.fooTableView selectRowIndexes:[NSIndexSet indexSetWithIndex:row] byExtendingSelection:NO];
[self refreshSelection];
}
So I am trying to select objects from my array, to be able to delete them when I do an IBAction. I tried:
checking if the item is Selected:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
if (self.editEnabled) {
RDNote *selectedNote = [self.notes objectAtIndex:indexPath.row];
if (selectedNote.isSelected) {
selectedNote.selected = NO;
for (NSIndexPath *indexPathFromArray in self.indexPathsOfSelectedCells) {
if (indexPathFromArray.row == indexPath.row) {
[self.mutableCopy removeObject:indexPathFromArray];
}
}
} else {
selectedNote.selected = YES;
[self.indexPathsOfSelectedCells addObject:indexPath];
}
[self.collectionView reloadData];
IBAction:
- (IBAction)didTapTrashBarButton:(id)sender {
NSMutableArray *mutableNotes = [NSMutableArray arrayWithArray:self.notes];
for (NSIndexPath *indexPath in self.indexPathsOfSelectedCells) {
[mutableNotes removeObjectAtIndex:indexPath.row];
}
self.notes = [NSArray arrayWithArray:mutableNotes];
[self.collectionView performBatchUpdates:^{
[self.collectionView deleteItemsAtIndexPaths:[NSArray arrayWithArray:self.indexPathsOfSelectedCells]];
} completion:^(BOOL finished) {
self.indexPathsOfSelectedCells = nil;
[self activateEditMode:NO];
[self saveDataToFile:self.notes];
}];
}
But I am having issues with indexes e.g.:(some times shows me the Error that Object index 2 is not between [0..1]), and there are bugs while selecting multiple Object and Deleting them.
Please help me with an advice for some other method that I can use, a Code will be perfect! Thanks!
This problem arrises because:
Let say you have five objects in array 1,2,3,4,5
you are running a loop for removing object based on indexpath which are selected rows. Now your indexpath contains 1st row and 3rd row.
while executing it for the first time you will remove object 1. Now 2,3,4,5 will left in the array. Now second time your indexpath.row is 3rd. It will remove third object which is 4 but in actual array it was 3.
Your code is crashing sometimes because if you have selected first row and last row. In this case i have selected 1 and 5. Now my indexpaths array will say I have to delect objectsAtIndexes 1 and 5.
While executing the loop I'll remove object at Index 1. Now i will be left with 2,3,4,5. On second iteration it will say remove objectAtIndex 5 where as Index 5 doesn't exist because now we have 4 elements in array.
In such cases best way of doing this is try removing elements in array from the end like remove 5th element first and then go for other one. Run your loop in reverse order.
NSInteger i = [self.indexPathsOfSelectedCells count]-1;
while (i > 0){
NSIndexPath *indexPath = [self.indexPathsOfSelectedCells objectAtIndex:i];
[mutableNotes removeObjectAtIndex:indexPath.row];
i--;
}
I am having an issue selecting appropriate consumables that I have set up in iTunes Connect to appear in my table in a nib file. (Using Ray Wenderlich's tutorial)
I have set up 11 consumables which are in two distinct sets; coins and categories.
What I want to do is have two buttons, each taking the user to a different table. Depending on the table a different set is displayed.
I have tried for hours to get this to work and I cannot do it; so I bow to the powers of the StackOverflow community to help.
Currently my set is as follows:
NSSet * productIdentifiers = [NSSet setWithObjects:
#"com.ac.scramble.coins1",
#"com.ac.scramble.coins2",
#"com.ac.scramble.coins3",
#"com.ac.scramble.coins4",
#"com.ac.scramble.coins5",
#"com.ac.scramble.category1",
#"com.ac.scramble.category3",
#"com.ac.scramble.category5",
#"com.ac.scramble.category8",
#"com.ac.scramble.category15",
#"com.ac.scramble.category30",
nil];
sharedInstance = [[self alloc] initWithProductIdentifiers:productIdentifiers
which is set up through iTunes Connect with no problem.
The evident problem here is that in my first nib which wants to show 6 rows, it is showing the final six in the list above (not sure why the final six rather than the first six...). This is fine, but in the second nib, which wants to show 5 rows, it is showing the final five from the list above. I don't want that - I want it to display the top five (the coins).
I've tried splitting the NSSet up into two and passing through but I cannot get that to work. I don't know if I can in the code somewhere specify which rows of the set I want to display. The pertinent code (I believe) which is probably where I need to do some trickery is:
- (void)reload {
_products = nil;
[self.table2 reloadData];
[[RageIAPHelper sharedInstance] requestProductsWithCompletionHandler:^(BOOL success, NSArray *products) {
if (success) {
_products = products;
[self.table2 reloadData];
}
//[self.refreshControl endRefreshing];
}];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableCell";
SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"SimpleTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
SKProduct *product = (SKProduct *) _products[indexPath.row];
cell.nameLabel.text = product.localizedTitle;
NSLog(#"Localized title: %#", product.localizedTitle);
cell.thumbnailImageView.image = [UIImage imageNamed:[thumbnails3 objectAtIndex:indexPath.row]];
cell.descriptionLabel.text = [descriptions3 objectAtIndex:indexPath.row];
[_priceFormatter setLocale:product.priceLocale];
//cell.detailTextLabel.text = [_priceFormatter stringFromNumber:product.price];
cell.progressLabel.text = [_priceFormatter stringFromNumber:product.price];
cell.descriptionLabel.text = product.localizedDescription;
}
Thanks to all in advance.
I managed to work out the answer and it was indeed splitting the set up. This may not be the most efficient way of doing this but I took the Set of 11 elements and depending on which one I wanted I either used the removeLastObject or removeObjectAtIndex[0] methods to obtain what I wanted. Thanks to everyone's help!
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! ;-)
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);