2014-09-13 2 views
0

내 테이블보기에있는 사용자 정의 UITableCell 내에서 UITextField에서 textFieldShouldReturn을 호출하려고합니다. 나는 셀의 인덱스 경로를 잡기 위해 코드의이 비트를 사용, 나는 또 다른 게시물에 유래에이 방법을 발견사용자 정의 UITableCell 내의 UITextField에서 textFieldShouldReturn을 호출하는 데 문제가 있습니다.

NSIndexPath *indexPath = [self.tableView indexPathForCell:(friendTableCell*)[[textField superview] superview]]; 

나는 오류를 가지고, 내가 컴파일 할 때와 실행하지 않는,하지만 내가 선택하는 경우 셀을 입력하고 텍스트를 입력 한 다음 키보드에서 반품을 시도하면 아무 일도 일어나지 않습니다. 오히려 길기 때문에 호출해야하는 코드의 나머지 부분은 제외했습니다. 그리고 didSelectRowAtIndexPath에서 테스트했으며 작동합니다. 누군가가 정말로 보살펴 주면 그것을 포함시킬 수 있습니다. 아마도 내 대리자가 키보드에 제대로 설정되지 않았지만 내 머리글에 UITextFieldDelegate가 포함되어 있다고 생각하고 실제로 사용자 지정 셀에 액세스 할 때 키보드의 대리자를 설정하는 방법을 잘 모르겠습니다. 테이블 뷰. 누군가가해야 할 일에 대한 아이디어 나 다른 방법을 가지고 있다면 정말 감사 할 것입니다. 감사!

UPDATE - 컨테이너 뷰와 셀 사이에 다른 층이있는 ScrollView가 완전 textFieldShouldReturn 방법

- (BOOL)textFieldShouldReturn:(UITextField *)textField 
{ 
    NSIndexPath *indexPath = [self.tableView indexPathForCell:(friendTableCell*)[[[textField superview] superview] superview]]; 

    NSString *sectionTitle = [self.usernameSectionTitles objectAtIndex:indexPath.section]; 
    NSArray *sectionUser = [self.sortedUsernames objectForKey:sectionTitle]; 
    self.recipient = [sectionUser objectAtIndex:indexPath.row]; 
    friendTableCell *cell = (friendTableCell *)[self.tableView cellForRowAtIndexPath:indexPath]; 

    NSString *sendingUser = self.currentUser.username; 
    NSString *message = [NSString stringWithFormat:@"from %@", [sendingUser uppercaseString]]; 

    if ([self.locationSwitch isOn]) 
    { 
     if ([cell.message.text length] > 0) 
     { 
      message = [NSString stringWithFormat:@"from %@ %@", [sendingUser uppercaseString],cell.message.text]; 
     } 

    CLLocation *location = locationManager.location; 
    if(!location) 
    { 
     [cell closeMessage:self]; 
    } 

    dispatch_queue_t queue = dispatch_queue_create("myQueue", DISPATCH_QUEUE_SERIAL); 
    dispatch_sync(queue, ^{ 
     CLLocationCoordinate2D coordinate = [location coordinate]; 
     PFGeoPoint *geoPoint = [PFGeoPoint geoPointWithLatitude:coordinate.latitude longitude:coordinate.longitude]; 
     PFObject *object = [PFObject objectWithClassName:@"Location"]; 
     [object setObject:geoPoint forKey:@"location"]; 
     [object setObject:self.recipient.objectId forKey:@"recipient"]; 

     [object saveEventually:^(BOOL succeeded, NSError *error) { 
      if(succeeded){ 
       UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Your Location was sent!"message:[error.userInfo objectForKey:@"error"] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
       [alertView show]; 

       dispatch_sync(queue, ^{ 
        PFQuery *locationQuery = [PFQuery queryWithClassName:@"Location"]; 
        [locationQuery whereKey:@"recipient" equalTo:self.recipient.objectId]; 

        /****** CHANGE THE getFirstObjectInBackground method so it doesn't take the first each time 
        or DELETE THE LOCATION after it has been viewed **************/ 

        [locationQuery getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) { 
         if (error) { 
          NSLog(@"Error: %@ %@", error, [error userInfo]); 
         } 
         else { 
          self.locationId = object.objectId; 
          NSLog(@"Here is you location ID: %@", self.locationId); 

          dispatch_sync(queue, ^{ 
           self.data = @{ 
               @"alert":message, 
               @"locationId":self.locationId 
               }; 

           PFQuery *userQuery = [PFUser query]; 
           [userQuery whereKey:@"objectId" equalTo:self.recipient.objectId]; 
           PFQuery *query = [PFInstallation query]; 
           [query whereKey:@"currentUser" matchesQuery:userQuery]; 

           PFPush *push= [[PFPush alloc]init]; 
           [push setQuery:query]; 
           [push setData:self.data]; 
           [push sendPushInBackground]; 
          }); 
         } 
        }]; 
       }); 
      } 
     }]; 
    }); 
} 



else if ([cell.message.text length] > 0) 
{ 
    message = [NSString stringWithFormat:@"from %@ %@", [sendingUser uppercaseString],cell.message.text]; 

    PFQuery *userQuery = [PFUser query]; 
    [userQuery whereKey:@"objectId" equalTo:self.recipient.objectId]; 
    PFQuery *query = [PFInstallation query]; 
    [query whereKey:@"currentUser" matchesQuery:userQuery]; 

    PFPush *push= [[PFPush alloc]init]; 
    [push setQuery:query]; 
    [push setMessage:message]; 
    [push sendPushInBackground]; 
} 

else 
{ 
    PFQuery *userQuery = [PFUser query]; 
    [userQuery whereKey:@"objectId" equalTo:self.recipient.objectId]; 
    PFQuery *query = [PFInstallation query]; 
    [query whereKey:@"currentUser" matchesQuery:userQuery]; 

    PFPush *push= [[PFPush alloc]init]; 
    [push setQuery:query]; 
    [push setMessage:message]; 
    [push sendPushInBackground]; 
} 

NSLog(@"Message sent!"); 
[cell closeMessage:self]; 
[cell resignFirstResponder]; 
return YES; 

}는

+0

당신이 당신의 텍스트 필드에 대한 대리자를 결합했다 .. 당신은 당신이이 일을 할 [textField resignResponder] 희망을 사용해야 대신 [cell resignFirstResponder];를 작성했습니다 볼 수 있습니까? 'textField.delegate = self'? – Vidhi

+0

네, 텍스트 필드는 위젯이 custom uitablecell nib 파일에 설정되어 있습니다. – user3353890

+0

CustomCell 또는 tableView 클래스에서 textFieldShouldReturn에 액세스하려는 위치는 어디입니까? –

답변

0

NSIndexPath *indexPath = [self.tableView indexPathForCell:(friendTableCell*)[[[textField superview] superview] superview]];보십시오. 사용자 정의 테이블 셀에 추가 할 때

또한 내가

+0

Ok ... 처음에는 작동했지만 그 다음으로 작동했습니다. 멈췄다. 그게 무슨 뜻인지는 모르겠다. 너는 어떤 생각을 가지고 있니? – user3353890

+0

나는 당신이하는 말을 잘 모르겠다. 그것이 효과가 있었고 멈췄다. 어떻게하면 가능한가, 그러면 효과가있다면 영원히 일하게 될 것이다. 다시 깨끗하게 지어 라. 또한 작동하지 않는 경우 대답을 수락하는 것을 잊지 :) 또한 세부 코드를 게시하십시오. – Mihir

+0

그래서 나는 너무 혼란 스럽다. 나는 처음에 그것을 내 폰에로드했는데 정상적으로 작동했습니다. 그런 다음 전혀 관련이없는 또 다른 코드에 대해 작업하고, 빌드를 다시 실행하고 내 앱을 사용해 보았을 때 "보내기"버튼이 더 이상 작동하지 않았습니다. 내 전체 textFieldShouldReturn 메서드를 포함하도록 위의 코드를 업데이트했습니다. 도와 주셔서 감사합니다! – user3353890

관련 문제