1

나는 여러 가지 방법으로 사용자가 한꺼번에 편집 할 수있는 매우 가변적 인 테이블 뷰를 가지고 있습니다. 이 셀의 텍스트 필드에서 행을 추가, 삭제 및 이름을 바꿉니다. 지금까지 내가 함께 셀 indexPath를 액세스 한 : 특정 객체가있는 테이블 뷰 셀에 액세스 중 : Superview를 통해 셀에 액세스하는 법적?

-(void)textFieldDidEndEditing:(UITextField *)textField 
{ 
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:textField.tag inSection:0]; 
    MainCategory *mainCategory = [self.fetchedResultsController objectAtIndexPath:indexPath]; 

    if(textField.text != mainCategory.name){ 
     mainCategory.name = textField.text; 
    } 

    self.activeField = nil; 
} 

그러나이, 삭제 등 재주문 후 문제를 준 지금이 방식으로 작동합니다 :

-(void)textFieldDidEndEditing:(UITextField *)textField 
{ 
    //NSIndexPath *indexPath = [NSIndexPath indexPathForRow:textField.tag inSection:0]; 

    // Get the cell in which the textfield is embedded 
    id textFieldSuper = textField; 
    while (![textFieldSuper isKindOfClass:[UITableViewCell class]]) { 
     textFieldSuper = [textFieldSuper superview]; 
    } 

    UITableViewCell *cell = textFieldSuper; 
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; 
    MainCategory *mainCategory = [self.fetchedResultsController objectAtIndexPath:indexPath]; 

    if(textField.text != mainCategory.name){ 
     mainCategory.name = textField.text; 
    } 

    self.activeField = nil; 
} 

이 그것을 할 수있는 법적 방법이 있나요 ?

+0

그래, 그건 괜찮습니다. – Martol1ni

+0

예, 문제가 아닙니다. 나는 여러 번 슈퍼 뷰를 사용했다. – santhu

+0

좋아요. 그래서 모든 태그를 없애고 있습니다 ... 문제가 많이 발생했습니다 ... THX! – MichiZH

답변

1

작동하지만 필요하지 않은 경우 루프를 사용하지 않는 것이 좋습니다. 당신은이 솔루션을 사용할 수

:

-(void)textFieldDidEndEditing:(UITextField *)textField 
{ 
     CGPoint pnt = [self.tableView convertPoint:textField.bounds.origin fromView:textField]; 
     NSIndexPath* indexPath = [self.tableView indexPathForRowAtPoint:pnt]; 

     MainCategory *mainCategory = [self.fetchedResultsController objectAtIndexPath:indexPath]; 

     if(textField.text != mainCategory.name){ 
      mainCategory.name = textField.text; 
     } 

     self.activeField = nil; 
} 
관련 문제