2009-10-26 7 views
5

테이블에서 행을 선택할 때 tableview에 새 행을 추가해야합니다.iphone의 TableView에서 행을 동적으로 추가하십시오.

나는 다음과 같은 방법을 사용합니까?

- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation 

새 행을 추가하는 방법에는 어떤 내용이 쓰여 있습니까?

사전 시빈에, 도와

감사를 수행하십시오.

당신이 배열에서 테이블을 작성하는 경우

답변

2

, 당신은 당신의 배열에 요소를 추가 할 수 있습니다 내가 동적으로 행을 추가 할 때, 나는 insertRowsAtIndexPaths를 사용 [myTable reloadData]

+0

아니요, 배열의 새 행 데이터를로드하지 않습니다. 필요한 것은 테이블의 특정 행을 선택할 때 새 행을 추가하는 것입니다. 기존 테이블에 추가됩니다. – smakstr

+0

흠. 그리고 귀하의 테이블 뷰 컨텐츠의 소스는 무엇입니까? – Morion

+1

numberOfRowsInSection에 의해 반환되는 항목의 수를 변경하고 cellForRowAtIndexPath에서 반환 한 데이터를 적절히 업데이트하면됩니다. 그런 다음 위와 같이 reloadData를 호출하십시오. –

13

전화 : 여기 예를 들어 기능이

- (void) allServersFound { 
    // called from delegate when bonjourservices has listings of machines: 
    bonjourMachines = [bonjourService foundServers]; // NSArray of machine names; 

    NSMutableArray *tempArray = [[NSMutableArray alloc] init]; 

    int i = 0; 
    for (NSArray *count in bonjourMachines) { 
     [tempArray addObject:[NSIndexPath indexPathForRow:i++ inSection:0]]; 
    } 

    [[self tableView] beginUpdates]; 
    [[self tableView] insertRowsAtIndexPaths:(NSArray *)tempArray withRowAnimation:UITableViewRowAnimationNone]; 
    [[self tableView] endUpdates]; 

    [tempArray release]; 
} 
0

ReloadData이 teble입니다 뷰에 행을 추가 할 수 있지만 일부 애니메이션을 원하는 경우 행이 다음 추가 할 때 이러한 라인을 사용한다 ... 좋은 간단하지만, 지금까지 내가 말할 수있는 애니메이션을하지 않습니다 암호.

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
     NSMutableArray *tempArray = [[NSMutableArray alloc] init]; 
     [tempArray addObject:[NSIndexPath indexPathForRow:indexPath.row+1 inSection:0]]; 

     [mainArray insertObject:@"new row" atIndex:indexPath.row+1]; 

     [[self tableView] beginUpdates]; 
     [[self tableView] insertRowsAtIndexPaths:(NSArray *)tempArray withRowAnimation:UITableViewRowAnimationFade]; 
     [[self tableView] endUpdates]; 

} 
관련 문제