iOS How can I take a random object from one array, and insert to another? **Help**

iJustinCabral

macrumors member
Original poster
Jul 8, 2012
58
0
I know about using something like

NSUInteger randomIndex = arc4random() % [theList count];

but I'm not sure how to set up the syntax between arrays to use something like that.

I also want to make it so the array from which the random object is taken, removes the object that is taken.

Can anyone out there help me out? Thanks in advance.
 

iJustinCabral

macrumors member
Original poster
Jul 8, 2012
58
0
This is what i was trying

I have two arrays

NSMutableArray *theList;
NSMutableArray *viewList;

Code:
-(IBAction)tapButton:(UIButton *)button
{

if (!.self.viewList)
{
self.viewList [[NSMutableArray alloc] init];
}

[hatList addObjectsFromArray:theList];
[theList removeAllObjects];

[self.collectionView reloadData];

}
I can't figure how to implement that arc4random in this situation.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
24,638
61
Harrogate
You can't using the methods you posted. You are adding all objects from one array then removing them from the source array. I suggest you look at the NSMutableArray documentation and look for methods to add an object and remove an object at a specified index.
 

iJustinCabral

macrumors member
Original poster
Jul 8, 2012
58
0
I was going to try something like

Paper *paperObject = [theList objectAtIndex:indexPath.row];

then just add the paper object to on array then remove from the other?
 

iJustinCabral

macrumors member
Original poster
Jul 8, 2012
58
0
Heres what I have now but now I need to convert this NSUInteger for my NSIndexPath

Code:
    if (!self.viewList)
    {
        self.viewList = [[NSMutableArray alloc]init];
        NSLog(@"viewList has been created");
    }
    
    NSUInteger randomIndex = arc4random() % [theList count];
    
    NSIndexPath *paperIndex = [NSIndexPath indexPathWithIndex:randomIndex];
    
    Paper *paperObject = [theList objectAtIndex:paperIndex];
    
    [hatList addObject:paperObject];
    [theList removeObject:paperObject];
    
    [self.collectionView reloadData];
I get this error "Incompatible pointer to integer conversion sending "NSIndexPath *__strong" to paramater of type 'NSUInteger'(AKA unsigned int)
 

iJustinCabral

macrumors member
Original poster
Jul 8, 2012
58
0
Nice..with what you said I realized I didn't need a NSIndexPath.

Thanks so much! Helping me fly through my debug stage.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
24,638
61
Harrogate
Nice..with what you said I realized I didn't need a NSIndexPath.

Thanks so much! Helping me fly through my debug stage.
Great :) Hopefully you can take this approach to the next problem. The more you do the less you'll find that you need help as the documentation will solve most of your problems.
 

iJustinCabral

macrumors member
Original poster
Jul 8, 2012
58
0
A problem I'm getting now is I cant get my cell to display the name of the text that was written into the object.

Code:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    IRHomeCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"IRHomeCell" forIndexPath:indexPath];
    NSLog(@"in cell for row");
    Paper *paperObject = [hatList objectAtIndex:indexPath.row];
    
    
    cell.name = (UILabel *)[cell viewWithTag:10];
    cell.number = (UILabel *)[cell viewWithTag:15];
    
    cell.name.text = paperObject.listNumber;
    cell.number.text = paperObject.listNumber;
    
    cell.backgroundColor = [UIColor whiteColor];
    [cell setNeedsDisplay];
    
    return cell;
}
The cell displays just a white background every time I launch, it's just the name that I can't seem to get to show up.

Any ideas?
 

Reason077

macrumors 68020
Aug 14, 2007
2,191
682
The cell displays just a white background every time I launch, it's just the name that I can't seem to get to show up.
You need to check for a nil result from dequeueReusableCellWithReuseIdentifier, and alloc a new cell if it didn't give you a reusable (cached) one.
 

iJustinCabral

macrumors member
Original poster
Jul 8, 2012
58
0
I thought with iOS 6 sdk CollectionViews & TableViews now you dont have to do that.

Instead you just register your cell class in the ViewControllers viewDidLoad.

Atleast thats what I thought.


I'll try your theory though and post back.
 
Last edited:

chown33

Moderator
Staff member
Aug 9, 2009
8,562
4,634
inter-prandial
I thought with iOS 6 sdk CollectionViews & TableViews now you dont have to do that.

Instead you just register your cell class in the ViewControllers viewDidLoad.

Atleast thats what I thought.


I'll try your theory though and post back.
Debugging is about testing your assumptions. That means breaking things down into individual testable assumptions, then testing them.

This is why learning to use a debugger is important. It lets you stop the program at a breakpoint, look at the actual values in variables, and step through. At each step (break things down), you look at the actual values (test your assumption) to see if it's correct (does the value match the assumption).

Even debugging with NSLog follows this pattern. Break things down into individual values (variables) you need to test. Then print the value. Printing only one NSLog for an entire sequence, and not even printing a variable, is not breaking things down. All you know is whether it reached that method or not. You know nothing about what's actually happening inside the method.

Learning a programming language without learning to use a debugger is like learning to fly by only learning how to take off, and not learning how to land. You have lots of crashes and no way to learn anything useful from your mistakes.

Learn to use the debugger now, on relatively simple code. It will actually make it easier to understand what your code is really doing, which means it's easier to learn how to write better code.
 

Reason077

macrumors 68020
Aug 14, 2007
2,191
682
Ahh, ok. You're using dequeueReusableCellWithReuseIdentifier:forIndexPath. This is new in iOS 6, and indeed shouldn't return nil.

First I'd check if it is indeed returning a valid cell. If not, maybe your cell class is not being registered properly. If it is returning a cell, there may be something wrong with your UITableViewCell implementation.
 

Duncan C

macrumors 6502a
Jan 21, 2008
853
0
Northern Virginia
A problem I'm getting now is I cant get my cell to display the name of the text that was written into the object.

Code:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    IRHomeCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"IRHomeCell" forIndexPath:indexPath];
    NSLog(@"in cell for row");
    Paper *paperObject = [hatList objectAtIndex:indexPath.row];
    
    
    cell.name = (UILabel *)[cell viewWithTag:10];
    cell.number = (UILabel *)[cell viewWithTag:15];
    
    cell.name.text = paperObject.listNumber;
    cell.number.text = paperObject.listNumber;
    
    cell.backgroundColor = [UIColor whiteColor];
    [cell setNeedsDisplay];
    
    return cell;
}
The cell displays just a white background every time I launch, it's just the name that I can't seem to get to show up.

Any ideas?
The code above does't make any sense to me. Can you post the definition of IRHomeCell?

At a glance, the code that assigns values to cell.name, cell.number, etc, seems odd. You're assigning objects to properties of the cell that come FROM the cell. What is that code supposed to do, set up the outlets for the cell object from views marked with tags? You can do that in the nibfile for the cell. Make the "files owner" for the XIB be the cell object itself, and link up the outlets to the appropriate views inside the cell, just like you do with a view controller.

Are you sure that [cell viewWithTag: x] is returning a value? For table views, you need to use [cell.contentView viewWithTag: x]

I've only looked at iOS 6 Collection views a little, and am not allowed to talk about them here in specifics because they are still under NDA.

(I don't understand how this forum can allow discussion of beta Apple OS'es, since our developer agreements expressly ban us from disclosing information about beta software releases to non-NDA recipients, and this is a public forum.)
 

iJustinCabral

macrumors member
Original poster
Jul 8, 2012
58
0
^^^^ Really??

Just a up and coming developer looking for help. No need to take this so serious man. Didn't realize I couldn't talk about whats in the SDK.
 

ArtOfWarfare

macrumors G3
Nov 26, 2007
8,579
4,019
^^^^ Really??

Just a up and coming developer looking for help. No need to take this so serious man. Didn't realize I couldn't talk about whats in the SDK.
Discuss such features on Apple's own forums until the SDK is released. The NDA pretty clearly states that you shouldn't talk about it on a publicly accessible forum like this one.