How to display selected consumables in a table in Objective C - objective-c

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!

Related

View based NSTableView text field loses focus

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

Selecting Objects from NSArray for further deleting with an IBAction

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

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

NSInternalInconsistencyException with UITableViewController and a Storyboard-Pop

I integrated GrabKit in my iPhone-App, which is a Library to Grab Photos from social Networks. Now I have the following situation/problem:
I have a UITableViewController - AlbumListViewController.
Then there's a UITableViewCell - AlbumCellViewController.
When you tap on one of these cells - albums with photos
There's a UITableViewController - PhotoListViewController
and a UITableViewCell - PhotoCellViewController.
Everything here works just finde, I can browse albums, choose one, browse the photos in it and then when I choose one of the single photos, there is a PushViewController to my SetPhotoViewController, which displays the selected image in Fullscreen.
Now when I want to use the back-Button of my navigation bar in the SetPhotoViewController, the crashes with the following message:
*** Assertion failure in -[UISectionRowData refreshWithSection:tableView:tableViewRowData:], /SourceCache/UIKit_Sim/UIKit-2372/UITableViewRowData.m:400
2012-10-22 22:49:32.814 Amis[1820:c07] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Failed to allocate data stores for 1073741821 rows in section 1. Consider using fewer rows'
*** First throw call stack:
(0x23de012 0x1b63e7e 0x23dde78 0x15f9f35 0xc8f83b 0xc923c4 0xb56fa2 0xb5692c 0x1690f 0x1cd653f 0x1ce8014 0x1cd87d5 0x2384af5 0x2383f44 0x2383e1b 0x2a9a7e3 0x2a9a668 0xaab65c 0x42fd 0x2b75)
libc++abi.dylib: terminate called throwing an exception
DataSource and Delegate of my two UITableViewControllers are both set to File's Owner.
When I debug trough the code, I can't find any special things, all photos can be loaded, all the arrays are filled with data, there's nothing strange.
Here are the two functions of the PhotoListViewController which get called, as soon as I press the back button on my NavigationController:
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
NSLog(#"%i", indexPath.row);
NSLog(#"%ii", indexPath.section);
// Extra Cell
if ( indexPath.section > _lastLoadedPageIndex ){
static NSString *extraCellIdentifier = #"ExtraCell";
cell = [tableView dequeueReusableCellWithIdentifier:extraCellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:extraCellIdentifier];
}
cell.textLabel.text = #"load more";
cell.textLabel.font = [UIFont fontWithName:#"System" size:8];
} else {
static NSString *photoCellIdentifier = #"photoCell";
cell = [tableView dequeueReusableCellWithIdentifier:photoCellIdentifier];
if (cell == nil) {
cell = [[[NSBundle mainBundle] loadNibNamed:#"PhotoRowViewController" owner:self options:nil] objectAtIndex:0];
}
}
// setting of the cell is done in method [tableView:willDisplayCell:forRowAtIndexPath:]
return cell;
}
and..
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
// extra cell
if ( [cell.reuseIdentifier isEqualToString:#"ExtraCell"] ){
if ( state == GRKDemoPhotosListStateAllPhotosGrabbed ) {
cell.textLabel.text = [NSString stringWithFormat:#" %d photos", [[_album photos] count] ];
}else {
cell.textLabel.text = [NSString stringWithFormat:#"Loading page %d", _lastLoadedPageIndex+1];
[self fillAlbumWithMorePhotos];
}
} else // Photo cell
{
NSArray * photosAtIndexPath = [self photosForCellAtIndexPath:indexPath];
[(PhotoRowViewController*)cell setPhotos:photosAtIndexPath];
}
}
What am I missing?
Edit: The code of the numberOfRowsInSection-Method:
If I debug it, the first time res is equal 0, the second time the method gets called, res is equal 322121535.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
NSUInteger res = 0;
if ( state == GRKDemoPhotosListStateAllPhotosGrabbed && section == _lastLoadedPageIndex ) {
NSUInteger photosCount = [_album count];
// Number of cells with kNumberOfPhotosPerCell photos
NSUInteger numberOfCompleteCell = (photosCount - section*kNumberOfRowsPerSection*kNumberOfPhotosPerCell) / kNumberOfPhotosPerCell;
// The last cell can contain less than kNumberOfPhotosPerCell photos
NSUInteger thereIsALastCellWithLessThenFourPhotos = (photosCount % kNumberOfPhotosPerCell)?1:0;
// always add an extra cell
res = numberOfCompleteCell + thereIsALastCellWithLessThenFourPhotos +1 ;
} else if ( section > _lastLoadedPageIndex) {
// extra cell
res = 1;
} else res = kNumberOfRowsPerSection;
return res;
}
I'm the developer of GrabKit. Thanks for using it :)
Actually, the controllers in GrabKit are there for demonstration purpose only, they are not designed to be used "as is" in a project, and more specifically : The GRKDemoPhotosList controller was not made to have another controller pushed in the controllers hierarchy.
So what you need is just a little fix to make grabKit demo's controllers fit to your project :)
I guess the problem is the following :
_ in [GRKDemoPhotosList viewWillAppear], the method fillAlbumWithMorePhotos is called.
_ when you pop back from your controller to GRKDemoPhotosList, this method is called one more time, and it generates this bug.
Try to add a flag in the viewWillAppear method, to avoid calling fillAlbumWithMorePhotos twice, I guess it'll work fine.
Feel free to contact me by mail ( pierre.olivier.simonard at gmail.com ) or on twitter ( #pierrotsmnrd ) if you need more informations or help :)

indexPath.row is always 0

I'm working on populating the table view with an array of dictionaries. The contents of the arrays and the dictionaries are parsed without problems. However, the table is populated with [array count] number of cells with contents with index 0 of the array. It seems to me that indexPath.row returns 0 for all cells. How do I populate each cell with the corresponding index?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
NSDictionary *term = [self.terms objectAtIndex:indexPath.row];
cell.textLabel.text = [term objectForKey:#"content"];
cell.detailTextLabel.text = [term objectForKey:#"created"];
return cell;
}
Thanks a lot!
Edit: Here's what I got for logging indexPath
2011-11-21 03:07:58.025 Demo[21269:f803] 2
indexes [0, 0]
2011-11-21 03:07:58.027 Demo[21269:f803] 2
indexes [1, 0]
Seems like the first index is updating while the second is not.
When I logged indexPath.row, however
2011-11-21 03:19:40.306 Demo[21546:f803] 0
2011-11-21 03:19:40.308 Demo[21546:f803] 0
So what should I use to get the value that is incrementing?
Thanks for the help again.
That block of code looks fine in itself. Maybe your tableview has multiple sections with one row each? What are you returning for the datasource methods tableView:numberOfRowsInSection: (should be [self.terms count]
) and numberOfSectionsInTableView: (should be 1).
If those are OK, put NSLog(#"%#",indexPath); somewhere in the method and post the output into your question.
I have just had a similar issue (indexPath.row was always 0) and noticed how much of an idiot I was. In my numberOfSectionsInTableView I was returning the [dataSet count] and in my tableView:numberOfRowsInSection I was returning 1. Should be the other way around. Oops!