2014-05-11 2 views
2

arc 프로젝트에 tableView가 있습니다. 내가 초 동안 심지어 그것을 스크롤하면 모든 데이터가 숨겨 지거나 사라집니다.UITableViewCell 스크롤 후 데이터가 사라집니다.

Strong 속성을 통해 다른 컨트롤러의 데이터를 전달하고 있습니다. 여기

CTableViewController* cTableVc=[[CTableViewController alloc] initWithNibName:@"CTableViewController" bundle:nil]; 
cTableVc.cArray=[[NSArray alloc] initWithArray:[MyVC pathForAllCardsInDocumentDiretory]]; 
cTableVc.view.frame=CGRectMake(0, 0, cTableVc.view.frame.size.width, cTableVc.view.frame.size.height); 
[self.view addSubview:cTableVc.view]; 

은 내 재산 문제는 당신이보기 컨트롤러를 생성하고 새로운 뷰 컨트롤러의 뷰를 추가 코드입니다

@property(strong, nonatomic) NSArray* cArray; 

CTableViewController.m있는 tableView 방법

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
// Return the number of sections. 
return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
// Return the number of rows in the section. 
return [_cardArray count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

NSString *CellIdentifier = @"CTableViewCell"; 
CTableViewCell *cell = (CTableViewCell *) [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 

    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CTableViewCell" owner:self options:nil]; 
    for (id currentObject in topLevelObjects){ 
     if ([currentObject isKindOfClass:[UITableViewCell class]]){ 
      cell = (CTableViewCell *) currentObject; 
      break; 
     } 
    } 
} 

// Configure the cell... 
NSString* strPath=[cArray objectAtIndex:indexPath.row]; 
cell.cardImageView.image=[UIImage imageWithContentsOfFile:strPath]; 
cell.cardNameLbl.text=[NSString stringWithFormat:@"My Card %d", arc4random() % 9]; 
cell.selectionStyle=UITableViewCellSelectionStyleNone; 


return cell; 
} 
+0

'cellForRowAtIndex :'는'cArray'에서 데이터를 사용하지 않습니다. 왜 안돼? – rmaddy

+0

실제로는 단순성을 위해 해당 행을 제거했습니다. 그것을 명확하게 편집. – NaXir

+0

코드의 첫 번째 세그먼트에서'[self.view cTableVc.view];는 무엇입니까? – sha

답변

21

입니다 하위보기. 그대로, 당신은 뷰 컨트롤러에 대한 참조를 유지하지 않습니다. 따라서 뷰 컨트롤러는 할당이 해제되고 테이블에는 대리자 또는 데이터 소스가 남아 있지 않습니다.

적절한 해결 방법은 UIViewController 컨테이너 메서드를 사용하고 새보기 컨트롤러를 현재보기 컨트롤러에 추가하는 것입니다.

전화 :

[self addChildViewController:cTableVc]; 

후 : 그냥이 한 줄보다 할 필요가 더 있기 때문에

self.view addSubview:cTableVc.view]; 

UIViewController의 문서를 참조하십시오.

+0

그런 우아한 대답, 고마워요! – Max

+0

@rmaddy, 감사합니다. 너는 내 하루를 구했다. – iKT

관련 문제