2014-04-10 2 views
0

을 호출하면 셀의 textField에서 indexPath를 가져 오는 방법이 내 첫 번째 질문입니다. 묻지는 않겠지 만 문서를 읽고 스택 오버플로에서 많은 코드를 시도한 후에도 난처한 상태입니다. 나는 최근에 코드를 배우기 시작 했으므로 근본적인 오류 (들)를 만들었다면 놀라지 않을 것입니다.didEndOnExit에서 사용자 정의 메서드

사용자 삽입 이미지 사용자 정의 셀의 textField에서 숫자를 입력하고 종료 할 때 saveGoal 메서드가 CustomCellClass.m (아래 코드)에서 호출됩니다. saveGoal 메서드는 이전 항목의 텍스트 파일에서 goalsArray를 만듭니다 (텍스트 파일이 만들어지지 않은 데이터가 없으면 작동하지 않으므로 생략했습니다). 이것은 어려운 곳입니다 (나를 위해). 메서드를 호출하는 셀의 indexPath가 필요하므로 사용자 항목을 삽입 할 수 있습니다 ... 그러나 셀 객체를 indexPathForCell의 매개 변수로 사용하는 방법을 알지 못합니다.

- (IBAction)saveGoal:(id)sender { 

    // Saving the user entry in a string 
    self.goalString = [NSString stringWithFormat:@"%@\n",self.goal.text]; 

    // Instance variables 
    NSError *err = nil; 
    NSMutableArray *goalsArray = [NSMutableArray arrayWithContentsOfURL:self.goalsURLMethod]; 

    // If there is data in the text file 
    NSIndexPath *indexPath = [[NSIndexPath alloc] init]; 

      // Ideally I'd like to do something like this (There is an IBOutlet from the 
      // table view in the storyboard to a tableView property). 
      indexPath = [self.tableView indexPathForCell: UNKNOWN CELL PARAMETER]; 

      // Then based on the indexPath I can insert or replace objects in the array 
      if(indexPath == 0) { 
      // masterful code 
      } else { 
      // If it's another row, replace the object. Something like this: 
      [goalsArray replaceObjectAtIndex: indexPath.row withObject: self.goalString]; 
      [goalsArray writeToURL:self.goalsURLMethod atomically:YES]; 

      // Then update the view 
      [self.tableView reloadData]; 
      } 
     } 
} 

해결책이 많이 있습니다.

@property (strong, nonatomic) NSIndexPath *indexPath; 

을하고 표시 셀을 반환 할 때 설정 :

답변

1

사용자 정의 셀이 경우에 속성을 추가 할 수 있습니다. 이제 셀은 필요한 정보에 액세스 할 수 있습니다. 이외에도

: 당신은 더 이상 그것을 사용하려고하고 있지만 이렇게하지 않는 것이

:

NSIndexPath *indexPath = [[NSIndexPath alloc] init]; 
indexPath = [self.tableView indexPathForCell: UNKNOWN CELL PARAMETER]; 

인스턴스를 생성 바로 다음 문장에서 그것을 파괴하는 것은 낭비이기 때문에

+0

감사, 일 가장 우아한 해결책 인 것 같습니다. 후속 질문이 있습니다. NSIndex 속성 (CustomCellClass의 customIndexPath)을 만들었습니다. 그런 다음 cellForRowAtIndexPath에서 CustomCellClass의 클래스 인스턴스를 만들고 다음과 같이 속성을 설정하려고했습니다. [CustomeCellClassInstance setCustomIndexPath : indexPath]; 그러나 NSLog customIndexPath 때 그것은 null을 반환합니다. 내가 뭘 놓치고 있니? –

+0

반환 할 셀 인스턴스가 이미 있어야합니다. 그것은 당신이 속성을 설정 한 인스턴스입니까? – Wain

0

또는 당신은 당신의 선택의 액션 메소드로 다음과 같이 그것을 할 단지 수 :

CGPoint hitPoint = [sender convertPoint:CGPointZero toView:self.tableView]; 
NSIndexPath *hitIndex = [self.tableView indexPathForRowAtPoint:hitPoint]; 
관련 문제