2016-02-19 14 views
11

Arka sayfadan yükleme verilerini sayfalama ile yapmaya çalışıyorum. Bunu gördüm, ancak tüm zamanları yükler. Bu doğru mu, yoksa yanlış bir şey mi yapıyorum?UITableView Sayfalandırması - Alttaki Yenileme Swift'deki Yeni Verileri Yükleme Yenile

Şimdiden teşekkürler. değil cellForRowAtIndexPath ama UIScrollViewDelegate daha fazla veri kontrol etmek

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{ 
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell 

    print(indexPath.row) 

    if (indexPath.row + 1 < self.TotalRowsWithPages) { 
     cell.textLabel?.text = "\(self.myarray!.[indexPath.row].body)" 
    } else { 

     cell.textLabel?.text = "Loading more data..."; 

     // User has scrolled to the bottom of the list of available data so simulate loading some more if we aren't already 
     if (!self.isLoading) { 
      self.isLoading = true; 
      self.TotalRowsWithPages = self.TotalRowsWithPages + self.PageSize 
      self.getmoredata() 
     } 
    } 

    return cell 
} 

cevap

29

Hayır, bu yaklaşıma devam edemez ve ayrıca sizin koşullarını kontrol etmek çok zaman alacak!

Burada UITableView sayfalandırma için daha iyi bir seçenek bulduk.

func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) { 

    //Bottom Refresh 

    if scrollView == tableView{ 

     if ((scrollView.contentOffset.y + scrollView.frame.size.height) >= scrollView.contentSize.height) 
     { 
      if !isNewDataLoading{ 

       if helperInstance.isConnectedToNetwork(){ 

        isNewDataLoading = true 
        getNewData() 
       } 
      } 
     } 
    } 
} 

isNewDataLoading o UITableView veya yeni veri yükleniyor kontrol etmek Bool olduğunu!

Bu yardımcı olur umarız!

+0

Swift 4'te çalışmaz –

+0

@UtkuDalmaz Bana tam olarak neyin işe yaramadığını söyler misiniz? –

+0

İkili işleç '==', 'UIScrollView' ve '_' türünde işlenenler için uygulanamaz.

1

deneyin - [DataModel sharedMyLibrary] sayfalama ile RESTful API kullanarak veri kaynağı sınıfı Video verileri yüklenirken, bu fetchWithCompletion yöntemi zaman uyumsuz içinde sunucudan veri almaya, öyle hasMore yöntemidir sunucu daha fazla veri (JSON sonraki bağlantı içerir) olduğunu söyledi LibraryTableViewController - UITableViewController alt sınıfı, hasMore - bir düğme içeren storyboard içinde tablo görünümü altındaki görünümüdür, böylece kullanıcı iki seçenek vardır: aşağı kaydırın veya buton. Ayrıca bu görünüm görünürse, sunucuda daha fazla veri olduğunu belirtir. _canFetch, sunucudan iç içe yüklemeyi engeller.

``

@interface LibraryTableViewController() <UIScrollViewDelegate> 
@property (weak, nonatomic) IBOutlet UIView *hasMore; 
@end 
@implementation LibraryTableViewController 
{ 
    __block volatile uint8_t _canFetch; 
} 
@synthesize hasMore = _hasMore; 
- (void)viewDidLoad 
{ 
    _canFetch = 0x80; 
    [super viewDidLoad]; 
    [self fetchVideos:NO]; 
} 
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset 
{ 
    CGPoint offset = [scrollView contentOffset]; 
    if ([[DataModel sharedMyLibrary] hasMore]) 
    { 
     if (((velocity.y > 0.0) && (offset.y > (*targetContentOffset).y)) || ((velocity.x > 0.0) && (offset.x > (*targetContentOffset).x))) 
     { 
      [self fetchVideos:NO]; 
     } 
    } 
    else 
    { 
     [_hasMore setHidden:YES]; 
    } 
} 
- (IBAction)moreVideos:(UIButton *)sender 
{ 
    [self fetchVideos:NO]; 
} 
- (IBAction)doRefresh:(UIRefreshControl *)sender 
{ 
    [sender endRefreshing]; 
    [[DataModel sharedMyLibrary] clear]; 
    [self fetchVideos:YES]; 
} 
- (void)fetchVideos:(BOOL)reload 
{ 
    if (OSAtomicTestAndClear(0, &(_canFetch))) 
    { 
     __weak typeof(self) weakSelf = self; 
     [[DataModel sharedMyLibrary] fetchWithCompletion:^(NSArray *indexPathes) { 
      dispatch_async(dispatch_get_main_queue(), ^{ 
       __strong typeof(self) strongSelf = weakSelf; 
       if (indexPathes != nil) 
       { 
        if (reload) 
        { 
         [[strongSelf tableView] reloadData]; 
        } 
        else 
        { 
         [[strongSelf tableView] beginUpdates]; 
         [[strongSelf tableView] insertRowsAtIndexPaths:indexPathes withRowAnimation:UITableViewRowAnimationAutomatic]; 
         [[strongSelf tableView] endUpdates]; 
        } 
       } 
       else 
       { 
        [[strongSelf tableView] reloadData]; 
       } 
       [strongSelf->_hasMore setHidden:![[DataModel sharedMyLibrary] hasMore]]; 
       strongSelf->_canFetch = 0x80; 
      }); 
     }]; 
    } 
} 

``

+1

Soru 'Swift''e aittir, bu yüzden gereksiz kod göndermeyin. Eğer 'Swift' koduna sahipseniz, o zaman bunun için gidin, aksi halde cevap –

+0

@ SohilR.Memon'u siliniz, adam yardım etmeye çalışıyor, evet 'Swift' değil, ama arkasındaki mantık aynı. – 0yeoj

+0

@ 0yeoj Ama hala adam tam kodu, kişinin nasıl anlayacağını yayınladı. Eğer mantığı açıklamaya çalışıyorsa o zaman cevapta sadece bu kısımdan bahsedilmelidir. Benim niyetim sadece soru soran kişinin çözdüğü problemi çözmektir. Yine de suç yok! –

5

Sen tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) daha yük uygulamalıdır. Son hücre yüklemesi görüntülendiğinde, kullanıcı aşağıya kaydırılır, bu yüzden daha fazla veri yüklemeye ihtiyaç duyduğunuz süredir.

Belki şuna benzer: cellForRowAtIndexPath defalarca denir çünkü

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { 

    if !(indexPath.row + 1 < self.TotalRowsWithPages) { 
     self.isLoading = true; 
     self.getmoredata() 

    } 
}