Iphone 从其他视图控制器访问阵列问题

Iphone 从其他视图控制器访问阵列问题,iphone,objective-c,ios,arrays,Iphone,Objective C,Ios,Arrays,我正在尝试从自定义UITableViewCell子类访问数组。阵列是在我的tableViewController中创建的。这让我快发疯了。我始终使用ViewController*vcInstance访问其他视图控制器的对象。我实际上需要从我的cell子类编辑数组,但我甚至不能从我的cell视图控制器中NSLog它。阵列完美地记录在我的tableViewController中。我从cell子类得到的是null CustomCell.h @property (retain, nonatomic) S

我正在尝试从自定义
UITableViewCell
子类访问数组。阵列是在我的tableViewController中创建的。这让我快发疯了。我始终使用
ViewController*vcInstance
访问其他视图控制器的对象。我实际上需要从我的cell子类编辑数组,但我甚至不能从我的cell视图控制器中
NSLog
它。阵列完美地记录在我的tableViewController中。我从cell子类得到的是
null

CustomCell.h

@property (retain, nonatomic) SongsViewController *vc;
CustomCell.m

@synthesize vc;

-(IBAction)setStateOfObjects
{
    NSMutableArray *array = [[NSMutableArray alloc] initWithArray:vc.parseTrackArray];
    NSLog(@"%@", array);
}
-(IBAction)setStateOfObjects
{
    SongsViewController *vc;
    NSLog(@"%@", vc.parseTrackArray);
}
我也尝试过:
CustomCell.m

@synthesize vc;

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

保留您的SongsViewController似乎是个坏主意。如果您使用的是iOS 5,那么这可能很弱,如果在iOS 5之前,请分配。您很可能会因此产生一个保留周期(内存泄漏)

在SongsViewController中创建CustomCell(可能在tableView:CellForRowatineXpath:中)时,是否设置了它的vc属性

[yourCell setVc:self];

编辑:您不太了解对象引用的工作原理。当您从数组中请求一个对象或“保留”该对象的其他对象时,您并没有创建新对象。因此,您不会以“以前的对象”和“更新的对象”结束。考虑这一点:

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
考虑到

你这样做的方式是倒退的。在数组的UITableVieCell子类中创建属性

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

当你创建单元格时,你设置了它的vc属性吗?你是说parseTrackArray吗?它在tableViewController中设置为属性。请参见下面的回答,但我只想指出,您不应该从UITableViewCell子类中引用另一个ViewController。这与最佳实践背道而驰,让事情变得相当混乱。理想情况下,您需要更多的封装,其中每个类对外部世界都非常不敏感,只处理它关心的数据和方法。在我的自定义单元格中,我将对象的状态添加到数组中。CellForRow不断查询数组中对象的状态。使用您的方法,我如何将数据传递回tableViewController?您不必这样做。如果在数组中添加、减去或更改对象,则应通过CustomCell类中的方法相应地更新CustomCell的标签和视图。您可以使用viewController中的键值观察来监视阵列中的更改,也可以在更改时发布可被viewController截获的通知。这就是我最后所做的。。。设置CustomCell的vcArray属性后,我修改了vcArray,并设置
tableViewController.parseTrackArray=vcArray
。这是正确的实现吗?好的,我明白了。您对对象和引用的工作方式有误解。您不必将任何内容发送回viewController,因为每个CustomCell都在查看同一个对象。您创建的每个CustomCell将查看和修改完全相同的数组。ViewController正在为tableView管理此阵列。