简体   繁体   English

Tableview滚动时不会加载更多数据

[英]Tableview not Load More Data When It Scrolled

i make an application that contain five view with segment controller my Views Name is NEWS,INTERVIEW,REVIEW,GALLERY etc.Here NEWS and INTERVIEW View Controller contain tableview with JSON Data Parsing when my application run then First load NEWSView when i first scroll it then it load more data but when segment InterviewViewController and First scroll it and then scroll a NEWS Table then it not load a more data please give me solution my Code is 我使用分段控制器制作了一个包含五个视图的应用程序,我的视图名称是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;
}

Add this scrollView Delegate method. 添加此scrollView Delegate方法。 Inside it add you code: 在其中添加代码:

Note: Do not give multiple request with getData method .(After get the request result after that only give other result- do it in your own idea) 注意:不要使用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