我正在尝试从自定义UITableViewCell
子类访问数组。该数组是在tableViewController中创建的。这真让我抓狂。我使用ViewController *vcInstance
始终访问其他视图控制器的对象。我实际上需要从我的单元子类编辑数组,但我甚至不能从我的单元视图控制器NSLog
。数组在tableViewController中完美记录。我得到的只是来自细胞亚类的null
。
CustomCell.h
@property (retain, nonatomic) SongsViewController *vc;
CustomCell.m
@synthesize vc;
-(IBAction)setStateOfObjects
{
NSMutableArray *array = [[NSMutableArray alloc] initWithArray:vc.parseTrackArray];
NSLog(@"%@", array);
}
我也尝试过:CustomCell.m
-(IBAction)setStateOfObjects
{
SongsViewController *vc;
NSLog(@"%@", vc.parseTrackArray);
}
答案 0 :(得分:1)
保留你的SongsViewController似乎是一个坏主意。如果您使用的是iOS 5,那么应该是弱的,如果在iOS 5之前,则分配。你可能会用它创建一个保留周期(内存泄漏)。
当你在SongsViewController中创建CustomCell(可能在tableView:cellForRowAtIndexPath :)时,你设置它的vc属性吗?
[yourCell setVc:self];
答案 1 :(得分:1)
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;
}