0

I'm trying to access an array from a custom UITableViewCell subclass. The array is created in my tableViewController. This is driving me crazy. I access other view controller's objects all the time with ViewController *vcInstance. I actually need to edit the array from my cell subclass, but I can't even NSLog it from my cell view controller. The array logs perfectly in my tableViewController. All I get is null from the cell subclass.

CustomCell.h

@property (retain, nonatomic) SongsViewController *vc;

CustomCell.m

@synthesize vc;

-(IBAction)setStateOfObjects
{
    NSMutableArray *array = [[NSMutableArray alloc] initWithArray:vc.parseTrackArray];
    NSLog(@"%@", array);
}

I've also simply tried: CustomCell.m

-(IBAction)setStateOfObjects
{
    SongsViewController *vc;
    NSLog(@"%@", vc.parseTrackArray);
}
3
  • 1
    When you create the cell, do you set its vc property? Commented Jun 1, 2012 at 17:43
  • Are you talking about parseTrackArray? It is set as a property in tableViewController. Commented Jun 1, 2012 at 17:46
  • See my answer below, but I just want to point out to you that you shouldn't be referencing another ViewController from within a UITableViewCell subclass. It goes against best practices and makes things pretty messy. You ideally want more encapsulation, where each class is pretty oblivious to the outside world and only deals with data and methods it cares about. Commented Jun 1, 2012 at 17:59

2 Answers 2

1

EDIT: You're not quite understanding how object referencing works. When you request an object from an array, or some other object that is "retaining" it, you are not creating a new object. So, you don't end up with your "previous object" and an "updated object". Consider this:

NSMutableDictionary *dict = [array objectAtIndex:index];
[dict setObject:@"Hello" forKey:@"Status"];
//You don't need to add *dict back to the array in place of the "old" one
//because you have been only modifying one object in the first place
[array replaceObjectAtIndex:index withObject:dict]; //this doesn't do anything

Considering that...

The way you're going about this is backwards. Make a property in your UITableVieCell subclass for your array

interface CustomCell : UITableViewCell
@property (nonatomic,retain) NSMutableArray *vcArray;
@end

#import CustomCell.h
@implementation CustomCell
@synthesize vcArray;

-(IBAction)setStateOfObjects { 
    NSMutableDictionary *dictionary = [parseTrackArrayToBeModified objectAtIndex:currentIndex]; 
    [dictionary setObject:[NSNumber numberWithBool:YES] forKey:@"sliderEnabled"]; 

    //THIS LINE IS REDUNDANT
    //[parseTrackArrayToBeModified replaceObjectAtIndex:currentIndex withObject:dictionary]; 
    //END REDUNDANT LINE

 }

//in your ViewController's delegate method to create the cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //assume previous code has CustomCell created and stored in variable
    CustomCell *cell;
    cell.vcArray = self.parseTrackArray;
    return cell;
}
Sign up to request clarification or add additional context in comments.

9 Comments

I would normally do it this way, but this array populates data in the table. In my custom cell, I'm adding the state of an object to the array. CellForRow is constantly querying the array for state of objects.
With your method, how would I pass data back to my tableViewController?
You don't have to. If you add, subtract, or change objects inside the array, you should update the CustomCell's labels and views accordingly, from methods within the CustomCell class. You could use Key-Value Observing in your viewController to monitor for changes in the array, or your could post NSNotifications that could be intercepted by your viewController when changes are made.
This is what I ended up doing... After setting the CustomCell's vcArray property, I then modified vcArray, and set tableViewController.parseTrackArray = vcArray. Is this proper implementation?
Ok, I see. You have a misunderstanding about the way objects and references work. You don't have to send anything back to the viewController, because each CustomCell is looking at the same object. Each CustomCell you make will be looking at and modifying the EXACT SAME array. The ViewController is managing this array for the tableView.
|
1

Retaining your SongsViewController seems like a bad idea. If you are using iOS 5, that should probably be weak, if before iOS 5, assign. You're likely creating a retain cycle (memory leak) with that.

When you create the CustomCell (probably in tableView:cellForRowAtIndexPath:) in SongsViewController, are you setting it's vc property?

[yourCell setVc:self];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.