应用程序在两分钟后崩溃

时间:2014-10-18 10:00:25

标签: ios uitableview

我正在创建一个从Web服务获取数据并在表视图中显示它的应用程序。当它启动时,它就像一个魅力,但当我滚动表格视图超过5到7倍的应用程序不起作用,它就像它被暂停。如果我没有在tableView上执行任何滚动,那么应用程序也会在2到3分钟后崩溃。

代码如下:

- (void)viewDidLoad
{
[self.interviewTable setShowsHorizontalScrollIndicator:NO];
[self.interviewTable setShowsVerticalScrollIndicator:NO];
pageNum=0;
[super viewDidLoad];

NSURL * url=[NSURL URLWithString:[NSString stringWithFormat:@"http://www.truemanindiamagazine.com/webservice/interview.php?page=%d",pageNum]];
NSData * data=[NSData dataWithContentsOfURL:url];

NSError * error;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingMutableContainers error: &error];
NSLog(@"%@",json);
self.dataArray=[json objectForKey:@"data"];
NSLog(@"images,%@",self.dataArray);
}
-(void)fetchedData:(NSData *)responsedata
{
NSError* error;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responsedata options:kNilOptions error:&error];
if ([[json objectForKey:@"data"] isKindOfClass:[NSArray class]])
{
    NSArray *newarray = (NSArray *)[json objectForKey:@"data"];
    [self.dataArray addObjectsFromArray:newarray];
    [self.interviewTable reloadData];
    NSLog(@"images,%@",self.dataArray);
}
if (self.dataArray.count == 0)
{
    self.interviewTable.scrollEnabled=NO;
}
else
{
    self.interviewTable.scrollEnabled=YES;
}

}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.dataArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (self.dataArray == nil || self.dataArray.count <1)
{
    return 0;
}
else
{
    return 1;
    return self.dataArray.count;
}
}
-(UITableViewCell *)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.dataArray objectAtIndex:indexPath.section];
NSString *img2=[dict valueForKey:@"post_image"];

[cell.inteviewImage sd_setImageWithURL:[NSURL URLWithString:[img2 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] placeholderImage:[UIImage imageNamed:@"Hisoka.jpg"] options:SDWebImageProgressiveDownload completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
    dispatch_async(dispatch_get_main_queue(), ^{
        NSLog(@"downloaded");

    });
}];
NSString *name=[dict valueForKey:@"post_title"];
cell.headLabel.text=name;
NSString *des=[dict valueForKey:@"post_content"];
cell.descripLabel.text=des;


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];
NSLog(@"Date %@",dateFormatted);
cell.dateLabel.text=dateFormatted;
return cell;
 }


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

}

当我设置一个断点时,它会在行中显示错误

NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responsedata options:kNilOptions error:&error];

请给我任何解决方案。

0 个答案:

没有答案