0

UITableViewCellカスタムサブクラスから配列にアクセスしようとしています。配列は私のtableViewControllerに作成されます。これは私を夢中にさせています。を使用して、他のViewControllerのオブジェクトに常にアクセスします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);
}
4

2 に答える 2

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;
}
于 2012-06-01T17:53:57.533 に答える
1

SongsViewControllerを保持することは悪い考えのようです。iOS 5を使用している場合は、iOS 5より前であれば、おそらく弱いはずです。これにより、保持サイクル(メモリリーク)が発生する可能性があります。

SongsViewControllerでCustomCell(おそらくtableView:cellForRowAtIndexPath :)を作成するとき、それをvcプロパティに設定していますか?

[yourCell setVc:self];
于 2012-06-01T17:47:23.847 に答える