2013-03-13 3 views
1

나는 다른 행/셀 있고 각 셀에 추가 단추가있는 TableView가있는 프로젝트를하고있다. 내가해야 할 일은 새 행이 버튼을 클릭 한 행 아래에 추가해야하는 추가 버튼을 클릭하면 행의 선택된 버튼 옆에 셀을 추가해야한다는 것입니다. 또한 사용자가 입력 할 수 있습니다. 새로운 행의 텍스트 어떻게하는지 말해줘. 나는 iPhone 개발에서 새로운 사람입니다. 나는 매우 감사 할 것이다 ....TableView 셀 추가

다음은 새로운 셀을 추가하는 동작을 수행하려는 클릭 버튼 기능입니다.

- (IBAction)AddButtonAction:(id)sender 
{ 
    [_choices insertObject:@"avav" atIndex:[_choices count]]; 
    [self.tableView reloadData]; 
    NSLog(@"here"); 

} 
+1

이것은 내가 onmy를 통합 할 수 아니라고 내가 이미 본 http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/tableview_iphone/ManageInsertDeleteRow/ManageInsertDeleteRow.html – Chandu

+0

을 시작 도움이 될 것입니다 프로젝트 – Priyanka

+0

모든 셀에 textfeild가 있습니까? – Durgaprasad

답변

2

이 대리자 메서드를 사용하면 선택한 행 아래에 새 행을 추가 할 수 있습니다. 그리고에 따라 사용자 정의 할이 enter image description here

필요 같은 것이

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if((selectedIndexPath) && (indexPath.row == selectedIndexPath.row) && (indexPath.section == selectedIndexPath.section)) 
    { 
     static NSString *[email protected]"cell"; 
     NewCell *cell = (NewCell *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

     if (cell == nil) 
     { 
      NSString *customeCellName = [NSString stringWithFormat:@"%@",[NewCell class]]; 

      NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:customeCellName owner:self options:nil]; 

      for (id currentObject in topLevelObjects) 
      { 
       if ([currentObject isKindOfClass:[UITableViewCell class]]) 
       { 
        cell = (NewCell *) currentObject; 
        break; 
       } 
      } 
     } 
     [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 


     return cell; 
    } 
    else 
    { 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
    cell.textLabel.text = [NSString stringWithFormat:@"%d",indexPath.row]; 

    // Configure the cell... 

    return cell; 
    } 
} 

출력처럼 cellforRowAtIndexPath 사용에 ... 그것에 텍스트 필드를 가지고

 rowCount ++;//Here rowcount refers the no. of rows in the table. 
    selectedIndexPath = indexPath;//Assign the selected indexpath for creating custom cell on it. 
    [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationBottom];//Indexpath refers the currently selected/targeted cell. 

을 사용자 정의 셀을 사용하여 요구 사항.

+0

좋은 답변 .. +1 – Dilip

+0

@ durgaprasad 아니오 버튼을 추가하고 버튼 클릭시 행을 추가 할 수 있으며 새 행 사용자는 텍스트를 입력 할 수 있습니다. – Priyanka

+0

이 코드를 사용해 보셨습니까? – Ganapathy

0

모든 세포에 textfeild가 있다고 생각합니다. AddButtonAction 함수에서 행의 인덱스를 저장합니다. 셀을 생성하는 동안 선택한 셀의 텍스트가 첫 번째로 반응합니다. 일단 사용자가 데이터를 배열에 저장하고 다시로드하십시오. selectedIndex를 올바르게 추적하면됩니다. 도움이 될 것입니다. 당신이 addSubview 방법으로 셀에 버튼을 추가하는 경우

0

, 간단하게 다음과 같은 코드로 indexPath를 얻을 수 있습니다 : 당신이 어떤 addSubview 수준을 사용하는 경우, 당신이있는 UITableViewCell에 액세스하기 위해 같은 superview 방법 역을 사용해야합니다

- (IBAction)AddButtonAction:(id)sender 
{ 
    UIButton *button = (UIButton *)sender; 
    UITableViewCell *cell = (UITableViewCell *)[button superview]; 
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; 

    // Adding methods comes here... 
} 

수평. 사용자 지정 셀을 사용하는 경우 위의 코드에서 사용자 지정 셀 클래스 이름을 UITableViewCell로 바꿉니다.

세포를 추가하려면 WWDC 2011 Videos, "UITableView Changes, Tips & 트릭"비디오를보십시오. insertRowsAtIndexPaths:withRowAnimation: 메서드와 같은 다른 행/섹션 사이에 새로운 행/섹션을 삽입, 삭제 및 다시로드하는 데 유용한 유용한 메서드를 보여줍니다. 또는 Apple Documents을 참조하십시오.

+0

감사합니다. @Amir Kh – Priyanka