2012-06-01 3 views
0

사용자 지정 UITableViewCell 하위 클래스에서 배열에 액세스하려고합니다. 배열은 내 tableViewController에서 만들어집니다. 이것은 나를 미치게 만든다. ViewController *vcInstance으로 항상 다른보기 컨트롤러의 객체에 액세스합니다. 실제로 내 셀 하위 클래스에서 배열을 편집해야하지만 심지어 내 셀보기 컨트롤러에서 NSLog 수 없습니다. 배열은 내 tableViewController에 완벽하게 기록됩니다. 내가 얻는 것은 셀 하위 클래스의 null입니다.다른보기 컨트롤러 문제에서 배열에 액세스

CustomCell.m

나는 또한 단순히 시도했습니다
@synthesize vc; 

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

@property (retain, nonatomic) SongsViewController *vc; 

CustomCell.h : 당신의 SongsViewController를 유지 CustomCell.m

-(IBAction)setStateOfObjects 
{ 
    SongsViewController *vc; 
    NSLog(@"%@", vc.parseTrackArray); 
} 
+1

셀을 만들 때 해당 VC 속성을 설정합니까? – executor21

+0

parseTrackArray에 대해 이야기하고 있습니까? 이 속성은 tableViewController의 속성으로 설정됩니다. – mnort9

+0

내 대답을 참조하십시오,하지만 난 당신이 UITableViewCell 하위 클래스 내에서 다른 ViewController 참조해서는 안된다는 것을 지적하고 싶습니다. 그것은 모범 사례에 어긋나고 일을 꽤 지저분하게 만듭니다. 이상적으로는 캡슐화가 더 필요합니다. 각 클래스는 외부 세계에 대해 잘 알지 못하고 관심있는 데이터와 메서드 만 처리합니다. – Justin

답변

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

나는 보통이 방법으로이 작업을 수행하지만이 배열은 테이블의 데이터를 채 웁니다. 내 사용자 정의 셀에서 객체의 상태를 배열에 추가하고 있습니다. CellForRow는 어레이의 객체 상태를 지속적으로 쿼리합니다. – mnort9

+0

귀하의 방법으로, 어떻게 다시 내 tableViewController 데이터를 전달할 것이라고? – mnort9

+1

꼭 할 필요는 없습니다. 배열 내에서 객체를 추가, 삭제 또는 변경하는 경우 CustomCell 클래스의 메서드에 따라 CustomCell의 레이블과 뷰를 적절히 업데이트해야합니다. viewController에서 Key-Value Observing을 사용하여 배열의 변경 사항을 모니터링하거나 변경이 이루어지면 viewController가 가로채는 NSNotifications를 게시 할 수 있습니다. – Justin

1

나쁜 생각처럼 보인다. iOS 5를 사용하고 있다면 iOS 5 이전에 약한 것 같습니다. 그걸로 유지주기 (메모리 누출)가 발생할 가능성이 높습니다.

SongSViewController에서 CustomCell (tableView : cellForRowAtIndexPath :)을 만들 때 vC 속성을 설정하고 있습니까?

[yourCell setVc:self]; 
관련 문제