2014-03-30 3 views
1

프로토 타입 셀을 사용하여 UITableView를 만들려고합니다. 이제 문제가 생겼어. 나는 3 종류의 다른 프로토 타입 셀을 사용하고 그 중 2 개에는 UITextField가 있습니다.UITableViewCell 및 UITextField

나는 UITableViewCell을 서브 클래 싱하여 필드를 벗어나는 정보를 얻기 위해 UITextFields의 콘센트를 2 회 서브 클래딩했다.

나는 올바른 방법으로하고 있습니까? 아니면 내 접근 방식을 다시해야합니까?

여기 내 코드 [업데이트]입니다 :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    static NSString *CellIdentifier; 

    switch (indexPath.section) { 
     case 0:{ 
      CellIdentifier = @"nameCell"; 
      naamCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
      if (cell == nil) { 
       cell = [[naamCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
      } 

      [cell setSelectionStyle: UITableViewCellSelectionStyleNone]; 

      return cell; 
      break; 
     } 

     default:{ 
      if(indexPath.row == [[_diceArray objectAtIndex:indexPath.section]count]){ 
       CellIdentifier = @"addCell"; 

      }else{ 
       CellIdentifier = @"optionCell"; 
       optionCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

       if (cell == nil) { 
        cell = [[optionCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
       } 
       [[cell optionCell] setText:[NSString stringWithFormat:@"%@", [[_diceArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]]] ; 
       [cell setSelectionStyle: UITableViewCellSelectionStyleNone]; 

       return cell; 
      } 

      break; 
     } 
    } 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    [cell setSelectionStyle: UITableViewCellSelectionStyleNone]; 

    return cell; 
} 

편집 : 그래서 여기

는 무슨 일이 일어나고 있는지의 사진입니다. 그래서 저는 2 개의 섹션을 가지고 있습니다. 내 텍스트 필드에 2 가지를 추가했습니다. 제 2 절의 UITextField에서 내용에서 하나의 섹션 3으로 이동 및 삭제되지 않고 삭제 Error

편집 2 : 나는 코드를 업데이트했습니다. 따라서 사람들은 UITextField를 동적으로 편집 할 수 있습니다. 내가 거기에 내 Array에서 제공된 NSStrings, 다음 Cell.textfield.text = 섹션의 인덱스 배열에서 텍스트를 테스트 할 생각 해요.

이제 사용자가 텍스트를 추가 할 수있는 방법을 찾고 텍스트 필드를 누르거나 변경하면 텍스트가 내 배열에 저장되고 [Table ReloadData]; 아니면 이것이 올바른 방법이 아닌가?

+0

당신은 당신이 각각에 대해 별도의 nib 파일을 생성 않았다, 당신의 세포를 서브 클래 싱 한 말? 이 경우, nibs를 등록해야 할 수도 있습니다 (아마도 viewDidLoad 메소드에서). 또한 프로토 타입 셀마다 스토리 보드에 다른 식별자가 있습니까? – romsearcher

+0

Storyboard에 프로토 타입 셀을 설치하고 사용자 정의 클래스를 부여하고 다른 식별자를 부여했습니다. – Kets

+0

문제가 분명하지 않습니다. 예상대로 작동하지 않는 것이 있습니까? 당신이 언급 한 문제는 무엇입니까? – stevesliva

답변

0

나는 마침내 그것을 얻었다 고 생각한다!

이를 구현하는 데 필요한 :

- (void) textFieldDidEndEditing:(UITextField *)textField { 
    UIView* v = textField; 

    do { 
     v = v.superview; 
    } while (![v isKindOfClass: [UITableViewCell class]]); 
    optionCell* cell = (optionCell*)v; 

    NSIndexPath* ip = [self._tabel indexPathForCell:cell]; 

    if (![textField.text isEqual:[[_diceArray objectAtIndex:ip.section] objectAtIndex:ip.row]]) { 
     [[_diceArray objectAtIndex:ip.section] setObject:textField.text atIndex:ip.row]; 
    } 
} 
관련 문제