2013-12-13 3 views
0

tableViewController에있는 tableView가있는 응용 프로그램이 있습니다. 사용자가 셀을 클릭 할 때 코드에 지정된 위치로 길 찾기를 제공하도록하려고합니다. 지금은 테스트를 위해 NSLog를 추가했습니다.테이블 뷰 셀을 클릭하면 경로가있는 맵이 열림

모든 위치 이름을 포함하는 tableData라는 viewDidLoad에 NSArray가 있습니다. 그것들은 셀로 올바르게 출력됩니다.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell * cell = nil; 

    cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell"]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MyCell"]; 
    } 

    cell.textLabel.text = [tableData objectAtIndex:indexPath.row]; 
    cell.detailTextLabel.text = [mainDelegate.distances objectAtIndex:indexPath.row]; 

    return cell; 
} 

문제는 아래 코드에 있습니다.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if ([[cell.textLabel.text] isEqualToString:@"Location 1") { 
     NSLog(@"Location 1 clicked"); 
    } 
} 

세포. in 문에서 "선언되지 않은 식별자 '셀'사용"오류가 발생하고 잘못된 점을 파악할 수 없습니다.

didSelectRowAtIndexPath: 방법에서
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 

    if ([[cell.textLabel.text] isEqualToString:@"Location 1"]) 
    { 
     NSLog(@"Location 1 clicked"); 
    } 
} 
+0

didSelectRowAtIndexPath에서, 당신은 당신이 셀의 뷰 객체를 사용하는 대신 cellForRowAtIndexPath에있는 것처럼 기본 데이터 소스 (tableData)를 사용합니다. – Anna

답변

0

기능 didSelectRowAtIndexPath 당신은이 작업을 수행 할 필요가있다 "세포"에 대한

을 모르는 선언되지 않았습니다. 셀 변수가있는 cellForRowAtIndexPath: 메서드의 셀 변수와 같지 않습니다. 이 코드에서

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

    if ([[cell.textLabel.text] isEqualToString:@"Location 1") { 
     NSLog(@"Location 1 clicked"); 
    } 
} 
+0

고맙습니다. 두 함수에서 별도로 선언해야한다는 것을 알지 못했습니다. – ibab

0

변수 cell : 또 다른 특종 (cellForRowAtIndexPath) 내에서 선언 이후

0

:

셀에 대한 참조를 얻으려면, 당신은 같은 것을 할 수

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 

    { 
     if ([[cell.textLabel.text] isEqualToString:@"Location 1") { 
      NSLog(@"Location 1 clicked"); 
     } 
    } 

셀은 어디 선언되지 않은, 아무것도 아니다. 당신이 누른 셀을 얻고 싶다면

, 이것을 사용 :

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    if ([[cell.textLabel.text] isEqualToString:@"Location 1") { 
     NSLog(@"Location 1 clicked"); 
    } 
0

당신은 그 방법 내부 셀 변수를 선언하지 않았습니다. 이 오류가 발생하는 이유는 무엇입니까?

은 교체 :

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if ([[cell.textLabel.text] isEqualToString:@"Location 1") { 
     NSLog(@"Location 1 clicked"); 
    } 
} 

으로 :

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell * cell = [self tableView:tableView cellForRowAtIndexPath:indexPath]; 
    if ([[cell.textLabel.text] isEqualToString:@"Location 1") { 
     NSLog(@"Location 1 clicked"); 
    } 
} 
관련 문제