簡體   English   中英

Tableview滾動時不會加載更多數據

[英]Tableview not Load More Data When It Scrolled

我使用分段控制器制作了一個包含五個視圖的應用程序,我的視圖名稱是NEWS,INTERVIEW,REVIEW,GALLERY等。這里,當我的應用程序運行時,NEWS和INTERVIEW View Controller包含帶有JSON數據解析的表視圖,然后在我第一次滾動時首先加載NEWSView它加載了更多數據,但是當段InterviewViewController並首先滾動它,然后滾動一個NEWS Table時,它沒有加載更多數據,請給我解決方案,我的代碼是

- (void)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];
[super viewDidLoad];
dispatch_async(kBgQueue, ^{
    data = [NSData dataWithContentsOfURL: url];
    [self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];
});
[self.spinner startAnimating];

}
-(void)fetchedData:(NSData *)responsedata
{
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
{
if (self.imageArray == nil || self.imageArray.count <1)
{
    return 0;
}
else
{
    return 1;
}
}
-(CustumCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *Cellidentifier=@"Cell";
CustumCell *cell=[tableView dequeueReusableCellWithIdentifier:Cellidentifier];
if (cell ==nil)
{
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustumCell" owner:self options:nil];
    cell = [nib objectAtIndex:0];
}
{
    NSDictionary *dict = [self.imageArray objectAtIndex:indexPath.section];
    NSString *img2=[dict valueForKey:@"post_image"];
    [cell.imagePhoto sd_setImageWithURL:[NSURL URLWithString:img2] placeholderImage:[UIImage imageNamed:@"Hisoka.jpg"]];


    NSString *title=[dict valueForKey:@"post_title"];
    cell.headLabel.text=title;

    NSString *contents=[dict valueForKey:@"post_content"];
    if([contents isKindOfClass:[NSString class]])
    {
        cell.descriptionLabel.text = contents;
    } else
    {
        cell.descriptionLabel.text =  @"";
    }
    NSDateFormatter * dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
    NSString *date=[dict valueForKey:@"post_date"];
    NSDate * dateNotFormatted = [dateFormatter dateFromString:date];
    [dateFormatter setDateFormat:@"d-MMM-YYYY"];
    NSString * dateFormatted = [dateFormatter stringFromDate:dateNotFormatted];
    cell.dateLabel.text=dateFormatted;


}
return cell;
}
-(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)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *dictionary=[self.imageArray objectAtIndex:indexPath.section];
NSString *url=[dictionary valueForKey:@"link"];
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:url]];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 80;
}

添加此scrollView Delegate方法。 在其中添加代碼:

注意:不要使用getData方法發出多個請求。(獲得請求結果之后,僅給出其他結果-請按照您自己的想法進行)

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
  NSInteger currentOffset = scrollView.contentOffset.y;
    NSInteger maximumOffset = scrollView.contentSize.height- scrollView.frame.size.height;
    if (maximumOffset - currentOffset <= 0) {

     // do your code here.
     [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];
});
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM