2012-11-19 2 views
0

두 개의 테이블 (셀 식별자 : 호출 및 셀 식별자 : 셀 2)이 있습니다. 두 테이블 (각 테이블에 대해 하나씩) 개체의 두 배열을 전달 해요 그러나 내 tableView 내 두 번째 테이블에이 작업을 수행 할 때 내 첫 번째 테이블 및 두 번째 배열의 데이터가 아닌 동일한 데이터를 가져 오는 것입니다. 내 배열은 내 .h 파일에서 NSMutableArray로 전역 적으로 설정됩니다. 그것은 첫째 return cell; 라인과 결코에 도달 할 때까지Objective C의 두 번째 테이블 채우기

작성된 코드, 간단하게 실행됩니다 :

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if(!cell) 
    { 
     cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    // NSString *currDate = [[array objectAtIndex:indexPath.row] objectForKey:@"Current Date"]; //added 
    // NSString *someOtherKey = [[array objectAtIndex:indexPath.row] objectForKey:@"Some other key"]; //added 


    cell.textLabel.text = [array objectAtIndex:indexPath.row]; 
    label1.text = [arrayDate1 objectAtIndex:indexPath.row]; 
    label2.text = [arrayDate2 objectAtIndex:indexPath.row]; 

    // cell.textLabel.text = [NSString stringWithFormat:@"%@ %@", currDate, someOtherKey]; //added 

    return cell; 



    //This will Load the second table (myTableView2) 
    static NSString *CellIdentifier2 = @"Cell2"; 


    UITableViewCell *cell2 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2]; 

    if(!cell2) 
    { 
     cell2 = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2]; 
    } 

    // NSString *currDate = [[array objectAtIndex:indexPath.row] objectForKey:@"Current Date"]; //added 
    // NSString *someOtherKey = [[array objectAtIndex:indexPath.row] objectForKey:@"Some other key"]; //added 


    cell2.textLabel.text = [array2 objectAtIndex:indexPath.row]; 


    return cell2; 
} 
+0

당신은 아래 코드는 실행되지 않습니다 처음 돌아 왔을 때. –

+0

두 번째 위의 첫 번째 수익을 이동하면 동일한 결과가 나타납니다. 어떻게 해결할 수 있습니까? – user1832095

답변

1

그것은이 기능에 문제가 참으로 : 나는 문제가 코드 내에서 생각하는 곳이다 그 후에 코드를 실행하면 항상 Cell 셀의 인스턴스가 반환됩니다.

이와 같이 두 개의 테이블을 사용하려면이를 구분할 수 있어야합니다. 일반적으로 둘 다 선언 된 속성에 저장합니다. 이 대답의 나머지 부분에 대해서는 내가하고 있다고 가정하고, table1table2이라고합니다. 당신 코드

변경은 다음과 같이합니다 :

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if ([tableView isEqual:self.table1]) { 
     static NSString *CellIdentifier = @"Cell"; 


     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

     if(!cell) 
     { 
      cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     } 

     // NSString *currDate = [[array objectAtIndex:indexPath.row] objectForKey:@"Current Date"]; //added 
     // NSString *someOtherKey = [[array objectAtIndex:indexPath.row] objectForKey:@"Some other key"]; //added 


     cell.textLabel.text = [array objectAtIndex:indexPath.row]; 
     label1.text = [arrayDate1 objectAtIndex:indexPath.row]; 
     label2.text = [arrayDate2 objectAtIndex:indexPath.row]; 

     // cell.textLabel.text = [NSString stringWithFormat:@"%@ %@", currDate, someOtherKey]; //added 

     return cell; 
    } else if ([tableView isEqual:self.table2]) { 


     //This will Load the second table (myTableView2) 
     static NSString *CellIdentifier2 = @"Cell2"; 


     UITableViewCell *cell2 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2]; 

     if(!cell2) 
     { 
      cell2 = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2]; 
     } 

     // NSString *currDate = [[array objectAtIndex:indexPath.row] objectForKey:@"Current Date"]; //added 
     // NSString *someOtherKey = [[array objectAtIndex:indexPath.row] objectForKey:@"Some other key"]; //added 


     cell2.textLabel.text = [array2 objectAtIndex:indexPath.row]; 


     return cell2; 
    } 

} 

// If you reach this point, we don't recognize the table and return `nil`, then the program will crash. Handle this however you want. 
return nil; 
+1

고마워, 정말 고마워! 완벽하게 작동합니다. – user1832095

+0

당신은 오신 것을 환영합니다! – lnafziger