2014-10-30 27 views
0

我做的,用JSON數據和WebServices包含多個網頁時我滾動UITableView的應用程序。這會提取並顯示下一組數據。下一頁由10組數據組成。但我只想展示其中的5個。這怎麼可能?如果可能,請分享代碼。負載幾種細胞當表視圖DidScroll在IOS

這裏是我的代碼:

-(void)scrollViewDidScroll:(UIScrollView *)scrollView 
{ 
    pageNum = pageNum + 1; 
    [self getData]; 
} 
-(void)getData 
{ 
    NSURL * url=[NSURL URLWithString:[NSString stringWithFormat:@"http://www.truemanindiamagazine.com/webservice/news.php?page=%d",pageNum]]; 
    dispatch_async(kBgQueue, ^{ 
     data = [NSData dataWithContentsOfURL:url]; 
     [self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES]; 
    }); 
} 

並獲取數據的方法代碼:

-(void)fetchedData:(NSData *)responsedata 
{ 
    if (responsedata.length > 0) 
    { 
     NSError* error; 
     self.json = [NSJSONSerialization JSONObjectWithData:responsedata options:kNilOptions error:&error]; 
     if ([[_json objectForKey:@"data"] isKindOfClass:[NSArray class]]) 
     { 
      NSArray *arr = (NSArray *)[_json objectForKey:@"data"]; 
      [self.imageArray addObjectsFromArray:arr]; 
      [self.newsTable reloadData]; 
      NSLog(@"images,%@",self.imageArray); 
     } 
    } 
    [self.spinner stopAnimating]; 
    self.spinner.hidesWhenStopped=YES; 
} 
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return self.imageArray.count; 
} 
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return 1; 
} 

而剩下的代碼是:

{ 
[super viewDidLoad]; 
pageNum=0; 
self.imageArray =[[NSMutableArray alloc]init]; 
NSURL * url=[NSURL URLWithString:[NSString stringWithFormat:@"http://www.truemanindiamagazine.com/webservice/news.php?page=%d",pageNum]]; 
[self.newsTable setShowsHorizontalScrollIndicator:NO]; 
[self.newsTable setShowsVerticalScrollIndicator:NO]; 

SDWebImageManager.sharedManager.imageDownloader.maxConcurrentDownloads = 3; 
SDWebImageManager.sharedManager.imageDownloader.executionOrder = SDWebImageDownloaderLIFOExecutionOrder; 

dispatch_async(kBgQueue, ^{ 
    data = [NSData dataWithContentsOfURL: url]; 
    [self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES]; 
}); 
} 

} 
-(CustumCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *[email protected]"Cell"; 
CustumCell *cell=[tableView dequeueReusableCellWithIdentifier:Cellidentifier]; 
if (cell ==nil) 
{ 
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustumCell" owner:self options:nil]; 
    cell = [nib objectAtIndex:0]; 
} 
{ 
    [cell.spinner startAnimating]; 
    NSDictionary *dict = [self.imageArray objectAtIndex:indexPath.section]; 
    NSString *img2=[dict valueForKey:@"post_image"]; 
    [cell.imagePhoto sd_setImageWithURL:[NSURL URLWithString:[img2 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] placeholderImage:[UIImage imageNamed:@"Setting.png"] options:SDWebImageHighPriority completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) 
    { 
      [cell.spinner stopAnimating]; 
      cell.spinner.hidesWhenStopped=YES; 

    }]; 
+0

返回這樣的numberofsections(self.imageArray.count> = 5),5:self.imageArray.count; – 2014-10-30 05:58:58

+0

我這樣做,那麼它顯示5號電池是好的,但是當我滾動表視圖然後沒有顯示更多的數據意味着更多的5號電池。 – 2014-10-30 06:13:56

+0

再次滾動後,你需要得到其餘5行,在這個時候你總需要正確顯示10行。 – 2014-10-30 06:17:12

回答

0

接受temparary數組alloc並嘗試將服務器中的數據添加到此arrray中。 現在我爲此執行此操作。

-(void)scrollViewDidScroll:(UIScrollView *)scrollView 
    { 

     if([mTenarray count]==0) { 
      pageNum = pageNum + 1; 
      [self getData]; 
     } else { 
      [self.imageArray addObjectsFromArray:mTenarray]; 
[mTenarray removeAllObjects]; 
      [self.newsTable reloadData]; 
     } 

    } 
    -(void)fetchedData:(NSData *)responsedata 
    { 
     if (responsedata.length > 0) 
     { 
      NSError* error; 
      self.json = [NSJSONSerialization JSONObjectWithData:responsedata options:kNilOptions error:&error]; 
      if ([[_json objectForKey:@"data"] isKindOfClass:[NSArray class]]) 
      { 
       NSArray *arr = (NSArray *)[_json objectForKey:@"data"]; 
       [self.mTenarray addObjectsFromArray:arr]; 
       NSArray *mfivedatarray = [self.mTenarray subarrayWithRange:NSMakeRange(0, 5)]; 
       [self.imageArray addObjectsFromArray:mfivedatarray]; 
       self.mTenarray // remove first five objects 
       [self.newsTable reloadData]; 
       NSLog(@"images,%@",self.imageArray); 
      } 
     } 
     [self.spinner stopAnimating]; 
     self.spinner.hidesWhenStopped=YES; 
    } 

在一些地方,我寫的概念來實現

+0

什麼是Self.mTenarry ????? – 2014-10-30 07:04:25

+0

數組在全局聲明以保存來自服務器的響應 – 2014-10-30 07:21:33