본문 바로가기
옛날

tableview row insert

by 차가운게 조아 2016. 9. 5.

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger maxPurchaseListCountInPage = 0;
    UIInterfaceOrientation orientation = [[UIApplicationsharedApplication] statusBarOrientation];
    if( [SystemInfo isPad] ){
        if( UIInterfaceOrientationIsLandscape(orientation) ){
            
        }else{
            
        }
    }else if( [SystemInfo isPhone5] ){
        if( UIInterfaceOrientationIsLandscape(orientation) ){
            
        }else{
            
        }
    }else{
        if( UIInterfaceOrientationIsLandscape(orientation) ){
            maxPurchaseListCountInPage = 5;
        }else{
            maxPurchaseListCountInPage = 8;
        }
    }
    
    if( maxPurchaseListCountInPage > [purchaseListArr count] )
        return;
    if( indexPath.row == [purchaseListArr count] -1)
        needMoreData = YES;
//        [self getPurChaseListMore];
}

위처럼 일단 willDisplayCell 가 불려 마지막 셀임을 감지 했을때, 더 데이터를 로드하기 위한 상태 플래그를 업데이트 시킨다.
이후 table view 가 실제로 scroll 이 마지막까지 이루어지고, 스크롤 애니메이션이 끝났을때 데이터를 더 로드한다.

로드 시점을 willDisplayCell 가 불렸을때 하지 않는 이유는, uitableview 와 같은 ui 의 경우, 딜레이가 없이 동작하기 위해, 실제 mutablearray 등이 업데이트 되기 전에 ui가 빠른 시점에 불려, 현재 로드된 데이터가 온전치 않은 상태가 발생 할 수 있다.
이는 특히 스크롤을 빠르게 할 경우등에 발생하며, 이 임계점에 다다르는 경우에는 tableview endupdates 메소드에서 crash 가 발생하는 경우가 생기게 된다.

위 문제 관련 stackoverflaw 
http://stackoverflow.com/questions/7751051/insertrowsatindexpaths-crashes-on-endupdates-in-tableviewwilldisplaycellforrow


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    ifneedMoreData == YES ){
        [selfgetPurChaseListMore];
    }
}

위에서 처럼 스크롤링 에니메이션이 끝나고, 데이터를 더 로드한다.

- (void)getPurChaseListMore
{
    AppDelegate *ad = (AppDelegate *)[[UIApplicationsharedApplication] delegate];
    if( [SystemInfo isPad] ){
        
    }else if( [SystemInfo isPhone5] ){
        
    }else{
        NSMutableArray *tempGetList = [ad.httpApi getPurchaseList:0];//page count 로변경해야할것.
        if( [tempGetList count] > 0 ){
            NSMutableArray *indexPaths = [[NSMutableArray allocinit];
            NSIndexPath *indexPath;
            forint i=0; i<[tempGetList count]; i++ )
            {
                indexPath = [NSIndexPath indexPathForRow:[purchaseListArr countinSection:0];
                [indexPaths addObject:indexPath];
                [purchaseListArr addObject:[tempGetList objectAtIndex:i]];
            }
            [purchaseTable beginUpdates];
            [purchaseTable insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationLeft];
            [purchaseTable endUpdates];
            [purchaseTable scrollToRowAtIndexPath:[indexPaths objectAtIndex:0] atScrollPosition:UITableViewScrollPositionBottomanimated:YES];
            ifselectAllState != e_selectDefault )
                selectAllState = e_selectDefault;
        }else{
            //더이상 구매 목록 없을 경우 오류 추가필요.
        }
    }
    needMoreData = NO;
}

데이터를 다 로드한 후에는 유저가 감지 할 수 있도록 scrollToRowAtIndexPath 메소드를 통해 스크롤 위치를 로드해서 더해진 객체중 첫번째 객체가 마지막 에 보일 수 있도록 조정한다.