2014-10-27 50 views
0

我做了一個應用程序,其中包含五個視圖與段控制器我的視圖名稱是新聞,採訪,審查,畫廊etc.Here新聞和採訪視圖控制器包含JSON數據解析tableview當我的應用程序運行,那麼首先負荷新聞瀏覽當我第一次把它的滾動,然後將其加載更多的數據,但是當段InterviewViewController和第一滾動,然後滾動新聞表則無法加載更多的資料,請給我解決我的代碼是Tableview不滾動時加載更多數據

- (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 *[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]; 
} 
{ 
    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; 
} 
+0

在這個實現中沒有問題。 – 2014-10-27 05:40:10

+0

你只需要理清你的tableview的源數組中的項數。 因爲你只需要做NSLog。 – 2014-10-27 05:41:41

+0

@DanialHussain我檢查它,但它是打印時,我把它的滾動它不是任何東西打印 – 2014-10-27 05:47:30

回答

0

添加此scrollView委託方法。 它裏面添加你的代碼:

注:不要給人以GetData方法多個請求(獲得請求的結果後,之後只給其他result-做你自己的想法)

-(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]; 
}); 
} 
相關問題