2012-02-16 2 views
1

하나의 응용 프로그램에서 두 인스턴스의 UITableView 있고 각 테이블을 표시해야하는 정의하는 방법을 모르는 문제입니다. 여기 코드는 다음과 같습니다두 TableView 처리하는 방법

.H :

{ 
NSArray *array1; 
NSArray *array2; 
IBOutlet UITableView *table1; 
IBOutlet UITableView *table2; 
} 

하는 .m :

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [array1 count]; 
} 

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    cell.textLabel.text = [array1 objectAtIndex:indexPath.row]; 
    return cell; 
} 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
} 

이 코드는 하나 개의 테이블에 대해 노력하고 있습니다. 배열 2로 다른 테이블을 설정하는 방법을 모르겠습니다. 아이디어 있니?

+0

나열된 모든 위임 된 메소드에는 tableView 매개 변수가 있습니다. tableView가 table1인지 table2인지 비교하여 비교할 수 있습니다. – user523234

답변

4

당신이해야 할 일은 delegate/datasource 방법에 어떤 UITableView을 설정하는지 확인하는 것입니다. 시도해보십시오.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; { 
    if(tableView == table1){ 
    return 1; // The number of sections in table1; 
    } 
else if(tableView == table2){ 
    return 1; // The number of sections in table2; 
    } 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; { 
    if(tableView == table1){ 
    return [array1 count]; 
    } 
    else if(tableView == table2){ 
    return [array2 count]; 
    } 
} 

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if(cell == nil){ 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 
    if(tableView == table1){ 
    cell.textLabel.text = [array1 objectAtIndex:indexPath.row];; 
    } 
    else if(tableView == table2){ 
    cell.textLabel.text = [array2 objectAtIndex:indexPath.row];; 
    } 
    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath; { 
    UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath]; 
} 

희망이 있습니다!

+0

objective-c에서 이와 같은 객체를 비교할 수 없습니다. isEqual : (id) 메서드를 사용해야합니다. 이와 같이 비교하는 것은 각 포인터의 주소를 비교하는 것입니다. –

+1

'isEqual : (id)'메소드는 실제로 객체를 검사하여 객체가 다른 객체와 같은지 확인하지만'=='는 두 객체의 포인터를 비교합니다. 그러나이 경우,'IBOutlet UITablveView * table1'은'UITableView'의 포인터에 대한 참조를 보유하므로,이 경우 포인터 검사는 유효 할뿐만 아니라 빠릅니다! – msgambel

+0

와우,이 모든 코드는 잘 작동합니다! 고마워요! – Adri

0

일반적인 방법은 각 테이블 뷰에 대해 별도의 데이터 소스/대리자 개체를 만드는 것입니다. 그들은 별도의 클래스가 될 필요가 없습니다. 같은 클래스의 두 인스턴스가 될 수 있습니다.

2

UITableViewDataSource 메서드에서 ivars를 대리자와 비교해야합니다. 이 같은

:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if([tableView isEqual:table1]) 
    { 
      return [array1 count]; 
    } 
    else if([tableView isEqual:table2]) 
    { 
      return [array2 count]; 
    } 
    else 
    { 
      return 0; 
    } 
} 

콜백의 모든 방법에 대해이 작업을 수행합니다.

그러나 하나의 tableView 만 있으면 일부 플래그를 기반으로 다른 내용을로드하는 것이 좋습니다. 이 기능을 수행하고 깃발을 설정하려면 [tableView reloadData]으로 전화해야합니다. 그런 다음 위 코드를 다음과 같이 변경하십시오. if([flag isEqualToString:@"table1"]) { //code for table1 }

동일한보기에 두 개의 테이블이없는 한. 그런 다음 첫 번째 방법은 당신이해야 할 일입니다.

관련 문제