2014년 5월 28일 수요일

append items to UITableView or UICollectionView


// from stack overflow with UITableView

// build the index paths for insertion
    // since you're adding to the end of datasource, the new rows will start at count
    NSMutableArray*indexPaths =[NSMutableArray array];
    NSInteger currentCount = self.datasource.count;
    for(int i =0; i < dataToAdd.count; i++)
    {
        [indexPaths addObject:[NSIndexPath indexPathForRow:currentCount+i inSection:0]];
    }
    // do the insertion
    [self.dataSource addObjects:dataToAdd];
    // tell the table view to update (at all of the inserted index paths)
    [self.tableView beginUpdates];
    [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationTop];

    [self.tableView endUpdates];



// in my real code with UICollectionView

- (void)getMoreServerData
{
// 새로운 아이템을 컬렉션의 끝에 추가
NSMutableArray *indexPaths = [NSMutableArray array];
                NSInteger currentCount = items.count;
                for (int i = 0; i < model.storesModelArray.count; i++) {
                    [indexPaths addObject:[NSIndexPath indexPathForRow:currentCount+i inSection:0]];
                }
                [items addObjectsFromArray:model.storesModelArray];
                [self.collectionView insertItemsAtIndexPaths:indexPaths];
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
// 리스트의 마지막에 도착한 경우 attach data
    if (scrollOffset + scrollView.frame.size.height == scrollView.contentSize.height)
    {
        [self getMoreServerData];
    }
}

댓글 없음:

댓글 쓰기