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

时间:2014-10-27 05:35:28

标签: ios json uitableview

我创建了一个包含五个视图的应用程序,带有段控制器我的视图名称是新闻,访谈,审查,画廊等。这里有新闻和访谈视图控制器包含带有JSON数据解析的tableview当我的应用程序运行时然后首先加载NEWSView滚动它然后它加载更多的数据,但当段InterviewViewController和第一次滚动它然后滚动一个新闻表然后它没有加载更多的数据请给我解决方案我的代码是

- (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;
}

1 个答案:

答案 0 :(得分:0)

添加此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];
});
}