2011-03-15 3 views
2

QTY 버튼 (빨간색 텍스트)을 누르고 같은 행의 텍스트 필드에 텍스트 (예 : 13)를 복사하고 싶습니다.도움말! uitableviewcell buttonpressed 업데이트 텍스트 필드

here is my sample uitableviewcell

-(IBAction)qtyButtonPressed:(id)sender { 

UITextField *textField = (UITextField *)[self.view viewWithTag:3]; 

textField.text = @"13"; 

이 내가 기압이 것입니다.

+0

관련 코드를 게시하면 더 많은 답변을 얻을 수 있습니다. – sarnold

답변

1

모든 셀에 버튼이있는 경우 먼저 어떤 행의 어떤 버튼을 클릭했는지 식별 할 수 있어야합니다. 이 1 개 섹션의 테이블은 일반적으로, 당신은 cellForRowAtIndexPath 내부의 버튼 태그 값으로 행 번호를 설정할 수 있습니다 셀이 버튼을 때

[button setTag:indexPath.row]; 

그런 다음 선택기에서 호출

볼 때 ... 설정 프레스, 행 번호를 결정하고,이 작업을 수행하려면 해당 행

int row = [sender tag]; 
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row section:0]; 
    id cell = [tableView cellForRowAtIndexPath:indexPath]; 
    [cell.textField setText:....]; 

에서 텍스트 필드에 텍스트를 설정하는 태그 값을 얻을, 당신은있는 UITableViewCell 하위 클래스, 재산/합성과 버튼에 textField에 액세스 할 수 있도록해야합니다.

+0

끝내 주셔서 감사합니다! – johnstontrav

0

UIControlEventTouchUpInside 단추에 addTarget:action:forControlEvents:을 사용하면 단추를 눌렀을 때 호출되는 선택기가 등록됩니다. 그런 다음 해당 메서드에서 해당 텍스트 필드를 찾아 text 속성을 지정합니다.

+0

감사합니다. Anomie, 저도 해봤지만, 해당 텍스트 필드를 "찾는 중"에 문제가 있습니다. 그 어떤 도움이라도? – johnstontrav

1

나는이 답변을 알고 있지만 불행히도 나는 InterfaceBuilder/Xcode에서 레이아웃을 허용하면서 여전히 다음과 같은 서브 클래 싱을 피할 수 있도록 테이블 뷰 셀 내의 필드를 찾기 위해 태그를 사용했다.

- (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath { 
    static NSString *AttributeCellIdentifier = @"AttributeCell"; 
    UITableViewCell *cell; 
    UILabel *label; 
    UITextField *value; 
    MyAttribute *a; 

    switch(indexPath.section) { 
     case ATTRIBUTES_SECTION: 
      cell = [tableView dequeueReusableCellWithIdentifier: AttributeCellIdentifier]; 
      label = (UILabel *) [cell viewWithTag: 1]; 
      value = (UITextField *) [cell viewWithTag: 2]; 
      a = [attributeList objectAtIndex: indexPath.row]; 
      label.text = a.label; 
      value.text = a.value; 
      break; 
     // Other sections... 
    } 
    return cell; 
} 

는하지만 내가 텍스트 필드가에있는 행에 대한 태그를 사용할 수 없음을 의미 내가 이렇게에 행 무엇을 볼 수있는 텍스트 필드에서 좌표를 사용 그래서 태그를 사용하는 대신 :.

- (void) textFieldDidEndEditing: (UITextField *) textField { 
    NSLog(@"Entering %s with %@", __func__, textField); 
    NSIndexPath *textFieldLocation = [self.tableView indexPathForRowAtPoint: [textField convertPoint:textField.bounds.origin toView: self.tableView]]; 
    NSLog(@"- The textfield is in the cell at: %@", textFieldLocation); 

    if(textFieldLocation.section == ATTRIBUTES_SECTION) { 
     MyAttribute *a = [attributeList objectAtIndex: textFieldLocation.row]; 
     a.value = textField.text; 
    } 
} 

텍스트 필드가 두 개 이상인 경우 셀에서 나는 태그 값을 사용하여 어느 것이 편집을 끝내는 지 알 수 있습니다.

심지어 어떤 보려면있는 tableview 인덱스를 반환하는 작은 도우미 메서드를 구축하는 스마트 수 있습니다 :이 필요한 코딩없이 범주에 넣어하고있는 tableview에 쉽게 사용할 수 할 수

- (NSIndexPath *) indexPathForView: (UIView *) view { 
    NSIndexPath *loc = [self.tableView indexPathForRowAtPoint: [view convertPoint: view.bounds.origin toView: self.tableView]]; 
    return loc; 
} 

.