Ios 水平滚动视图上未加载内容
嗨,我正在构建一个应用程序,我希望我的内容显示在一个水平滚动的视图上。 我已经为它实现了以下代码:Ios 水平滚动视图上未加载内容,ios,Ios,嗨,我正在构建一个应用程序,我希望我的内容显示在一个水平滚动的视图上。 我已经为它实现了以下代码: -(void)DownLoadData:(NSString *)indexSelect { self._parserForNewsDetail = [afaqsParser getInstance]; [[afaqsParser getInstance] setCacheNeed:TRUE]; [self._parserForNewsDetail parseWi
-(void)DownLoadData:(NSString *)indexSelect
{
self._parserForNewsDetail = [afaqsParser getInstance];
[[afaqsParser getInstance] setCacheNeed:TRUE];
[self._parserForNewsDetail parseWithUrl:[_arrUrlLinks objectAtIndex:[indexSelect integerValue]] UrlTypefor:nil];
NSDictionary *resultDic;
resultDic = [[[self._parserForNewsDetail getLinkAndIdDic] valueForKey:@"items"]objectAtIndex:0];
NSLog(@"Detail Dic = %@",[resultDic description]);
if (resultDic== NULL || resultDic ==nil)
{
//Check internet here
[[SharedUtilities getInstance]RemoveActivityIndicatorView];
[SharedUtilities ShowAlert:@"No Data Found" title:nil withView:self.view];
return;
}
[self performSelectorOnMainThread:@selector(SetValuesInUserInterface:) withObject: resultDic waitUntilDone:NO];
[[SharedUtilities getInstance]RemoveActivityIndicatorView];
}
-(void)DownloadNext:(NSString *)indexSelect
{
resultDic = [[[self._parserForNewsDetail getLinkAndIdDic] valueForKey:@"items"]objectAtIndex:0];
NSURL *imgurl =[NSURL URLWithString:[[Dic valueForKey:@"image"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSArray *subviewsArr = [self.view subviews];
for (int i=0; i<[subviewsArr count]; i++) {
if ([[subviewsArr objectAtIndex:i] isKindOfClass:[ImageDownLoader class]]) {
}
}
[self loadScrollView];
}
-(void)SetValuesInUserInterface:(NSDictionary *)Dic
{
self._imageView1.layer.cornerRadius = 4;
self._imageView1.clipsToBounds = YES;
self._imageView1.tag = 999;
NSURL *imgurl =[NSURL URLWithString:[[Dic valueForKey:@"image"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
self._imageView1.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:imgurl]];
NSArray *subviewsArr = [self.view subviews];
for (int i=0; i<[subviewsArr count]; i++) {
if ([[subviewsArr objectAtIndex:i] isKindOfClass:[ImageDownLoader class]]) {
[[subviewsArr objectAtIndex:i] removeFromSuperview];
}
}
if ([[Dic valueForKey:@"image"] isEqual:@""])
{
_imageView1.hidden=YES;
_txtView.frame=CGRectMake(4.0f,95.0f,_txtView.frame.size.width,_txtView.frame.size.height );
NSLog(@"NO IMAGE");
}
else{
_imageView1.hidden=NO;
_imageView1.frame=CGRectMake(4.0f,95.0f,310.0f,180.0f );
// _txtView.frame=CGRectMake(4.0f,316.0f,310.0f,159.0f );
_txtView.frame=CGRectMake(4.0f,316.0f,_txtView.frame.size.width,_txtView.frame.size.height );
NSLog(@"IMAGE VISIBLE");
}
self._scrollView.scrollEnabled = YES;
self._scrollView.showsVerticalScrollIndicator = YES;
self._scrollView.showsHorizontalScrollIndicator = YES;
self._header.font = [UIFont fontWithName:@"HelveticaNeue-MediumCond" size:18];
[self._header setText: [Dic valueForKey:@"header"]];
self._header.textColor=[UIColor blackColor];
[self._Writer setText:[Dic valueForKey:@"AUTHOR"]];
[self._kicker setText:[Dic valueForKey:@"kicker"]];
[self._txtView setText:[Dic valueForKey:@"ARTICLE_BODY"]];
NSString *writer;
if ([[Dic valueForKey:@"AUTHOR"] length]>2)
{
writer=[NSString stringWithFormat:@"%@, ",[Dic valueForKey:@"AUTHOR"]];
}
else
{
writer=@"";
}
[self._Writer setText:str];
[_txtView sizeToFit]; //added
[_txtView layoutIfNeeded]; //added
CGRect frame = self._txtView.frame;
self._txtView.frame = frame;
[_txtView setScrollEnabled:NO];
self._scrollView.contentSize = CGSizeMake(320,440+frame.size.height);
_titleLabel.frame= CGRectMake(0, self._scrollView.contentSize.height-119, [[UIScreen mainScreen] bounds].size.width, 40);
_titleLabel.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:1];
_titleLabel.textColor = [UIColor whiteColor];
_titleLabel.textAlignment = NSTextAlignmentCenter;
_titleLabel.font = [UIFont fontWithName:@"Helvetica" size:13.5];
_titleLabel.numberOfLines=2;
[self._scrollView addSubview:_titleLabel];
[self loadScrollView];
}
-(void)viewWillAppear:(BOOL)animated
{
self.navigationController.navigationBarHidden = YES;
}
- (void)viewDidLoad
{
[self DownLoadData:resultDic];
UISwipeGestureRecognizer *rightRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(loadScrollView)];
rightRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
[rightRecognizer setNumberOfTouchesRequired:1];
//add the your gestureRecognizer , where to detect the touch..
[_scrollView addGestureRecognizer:rightRecognizer];
UISwipeGestureRecognizer *leftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(loadScrollView)];
leftRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
[leftRecognizer setNumberOfTouchesRequired:1];
[_scrollView addGestureRecognizer:leftRecognizer];
}
- (void)rightSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer
{
[self btnPreviousClick];
[self loadScrollView];
}
- (void)leftSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer
{
NSLog(@"leftSwipeHandle");
[self btnNextClick];
[self loadScrollView];
}
-(IBAction)btnNextClick
{
btnPreviousNews.userInteractionEnabled=TRUE;
if(count!=[_arrUrlLinks count] -1)
{
if(count==[_arrUrlLinks count]-2)
{
btnNextNews.userInteractionEnabled=FALSE;
[btnNextNews setImage:[UIImage imageNamed:@"arrow2_next.png"] forState:UIControlStateNormal];
}
[btnPreviousNews setImage:[UIImage imageNamed:@"arrow1_prev.png"] forState:UIControlStateNormal];
count=count +1;
_lblNewsCount.text=[NSString stringWithFormat:@"%d/%d",count+1,[_arrUrlLinks count]];
NSLog(@"next %d",count);
[self performSelectorInBackground:@selector(DownLoadData:) withObject:[NSString stringWithFormat:@"%d",count]];
}
else{
btnNextNews.userInteractionEnabled=FALSE;
}
}
-(void)btnPreviousClick
{
btnNextNews.userInteractionEnabled=TRUE;
if(count==0)
{
btnPreviousNews.userInteractionEnabled=FALSE;
}
else{
if(count==1)
{
[btnPreviousNews setImage:[UIImage imageNamed:@"arrow2_prev.png"] forState:UIControlStateNormal];
btnPreviousNews.userInteractionEnabled=FALSE;
}
[btnNextNews setImage:[UIImage imageNamed:@"arrow1_next.png"] forState:UIControlStateNormal];
count=count-1;
_lblNewsCount.text=[NSString stringWithFormat:@"%d/%d",count+1,[_arrUrlLinks count]];
NSLog(@"previous %d",count);
[self performSelectorInBackground:@selector(DownLoadData:) withObject:[NSString stringWithFormat:@"%d",count]];
}
}
-(void)loadScrollView
{
_scrollView.contentSize = CGSizeMake(0, _scrollView.frame.size.height);
NSMutableArray *controllers = [[NSMutableArray alloc] init];
for (unsigned i = 0; i < [_arrUrlLinks count]; i++) {
[controllers addObject:[NSNull null]];
}
self.viewControllers = controllers;
count=1;
// a page is the width of the scroll view
_scrollView.pagingEnabled = YES;
_scrollView.contentSize = CGSizeMake(_scrollView.frame.size.width * [_arrUrlLinks count], _scrollView.frame.size.height);
_scrollView.showsHorizontalScrollIndicator =YES;
_scrollView.showsVerticalScrollIndicator = NO;
_scrollView.scrollsToTop = NO;
_scrollView.delegate = self;
pageControl.numberOfPages = [_arrUrlLinks count];
pageControl.currentPage = 0;
[_scrollView addSubview:_txtView];
[_txtView setText:[Dic valueForKey:@"ARTICLE_BODY"]];
[controller.view addSubview:_txtView];
// pages are created on demand
// load the visible page
// load the page on either side to avoid flashes when the user starts scrolling
[self loadScrollViewWithPage:0];
[self loadScrollViewWithPage:1];
}
- (void)loadScrollViewWithPage:(int)page {
if (page < 0) return;
if (page >= [_arrUrlLinks count])
return;
// replace the placeholder if necessary
controller = [viewControllers objectAtIndex:page];
if ((NSNull *)controller == [NSNull null]) {
NSString *deviceType = [UIDevice currentDevice].model;
if([deviceType isEqualToString:@"iPhone"])
{
controller = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];
}
else{
controller = [[MyViewController alloc] initWithNibName:@"MyViewController_ipad" bundle:nil];
}
[controller initWithPageNumber:page];
// [controller.view addSubview:_txtView];
// [controller.view addSubview:_imageview];
NSLog(@"loadscrollviewwithpage");
[viewControllers replaceObjectAtIndex:page withObject:controller];
// [controller release];
}
// add the controller's view to the scroll view
if (nil == controller.view.superview) {
CGRect frame = _scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
controller.view.frame = frame;
// _txtView.frame=frame;
// _imageview.frame=frame;
// _txtView.frame=CGRectMake(4.0f,316.0f,310.0f,159.0f );
_txtView=@"hello";
// [controller.view addSubview:_txtView];
[_imageView1 addSubview:controller.view];
// [_txtView addSubview:controller.view];
// [scrollView addSubview:controller.view];
NSLog(@"loadscrollviewwithpage................>>>>>>>>>>>>");
// [self._header setText: [Dic valueForKey:@"header"]];
// [self DownLoadData:resultDic];
//[self SetValuesInUserInterface:Dic];
[self DownloadNext:resultDic];
}
}
- (void)unloadScrollViewWithPage:(int)page {
if (page < 0) return;
if (page >= [_arrUrlLinks count]) return;
controller = [viewControllers objectAtIndex:page];
if ((NSNull *)controller != [NSNull null]) {
if (nil != controller.view.superview)
[controller.view removeFromSuperview];
[viewControllers replaceObjectAtIndex:page withObject:[NSNull null]];
// [controller.view addSubview:_txtView];
// [controller.view addSubview:_imageview];
NSLog(@"Unloadscrollviewwithpage");
// [[NSURLCache sharedURLCache] removeAllCachedResponses];
}
}
- (void)scrollViewDidScroll:(UIScrollView *)sender {
// We don't want a "feedback loop" between the UIPageControl and the scroll delegate in
// which a scroll event generated from the user hitting the page control triggers updates from
// the delegate method. We use a boolean to disable the delegate logic when the page control is used.
if (pageControlUsed) {
// do nothing - the scroll was initiated from the page control, not the user dragging
return;
}
// Switch the indicator when more than 50% of the previous/next page is visible
CGFloat pageWidth = _scrollView.frame.size.width;
int page = floor((_scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
pageControl.currentPage = page;
// NSLog(@"current page %d",page);
// load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)
[self unloadScrollViewWithPage:page - 2];
[self loadScrollViewWithPage:page - 1];
[self loadScrollViewWithPage:page];
[self loadScrollViewWithPage:page + 1];
[self unloadScrollViewWithPage:page + 2];
count=page+1;
// [self newCountTitleSet];
// A possible optimization would be to unload the views+controllers which are no longer visible
}
// At the begin of scroll dragging, reset the boolean used when scrolls originate from the UIPageControl
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollViewLoc
{
CGFloat pageWidth = scrollViewLoc.frame.size.width;
CGPoint translation = [scrollViewLoc.panGestureRecognizer translationInView:scrollViewLoc.superview];
int page = floor((scrollViewLoc.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
if(translation.x > 0)
{
if(_strCurrentNewsSelect!=0)
{
if(page==0)
{
NSLog(@"OK");
NSLog(@"scrollviewwillbegindragging=0");
[self DownLoadDataPrevious:[_strCurrentNewsSelect.integervalue]-1];
}
}
else{
if(page==1)
{
NSLog(@"previous button");
[self btnPreviousClick];
}
}
} else
{
// [_txtView setText:[Dic valueForKey:@"ARTICLE_BODY"]];
// [controller.view addSubview:_txtView];
// [controller.view addSubview:_txtView];
// [controller.view addSubview:_imageview];
NSLog(@"loadscrollviewwithpage else");
[self btnPreviousClick];
NSLog(@"previous news");
// if(galleryItemClick+1!=[arrGallaeryUrl count])
//
// {
//
if(page+1==[_arrUrlLinks count])
//
{
//
NSLog(@"====....>>>>>>>");
}
count=count+1;
}
}
-(void)下载数据:(NSString*)索引选择
{
self._parserfornewsdail=[afaqsParser getInstance];
[[afaqsParser getInstance]setCacheNeed:TRUE];
[self.\u parserfornewsdail parseWithUrl:[[u arrUrlLinks objectAtIndex:[indexSelect integerValue]]UrlTypefor:nil];
NSDictionary*resultDic;
resultDic=[[self.\u parserfornewsdail getlinkandidic]valueForKey:@“items”]objectAtIndex:0];
NSLog(@“详细Dic=%@,[结果描述]);
if(resultDic==NULL | | resultDic==nil)
{
//在这里查看互联网
[[SharedUtilities getInstance]RemoveActivityIndicatorView];
[SharedUtilities ShowAlert:@“未找到数据”标题:nil with view:self.view];
返回;
}
[self-performSelectorOnMainThread:@selector(SetValuesSinserInterface:)with object:resultDic waitUntilDone:NO];
[[SharedUtilities getInstance]RemoveActivityIndicatorView];
}
-(void)下载下一步:(NSString*)索引选择
{
resultDic=[[self.\u parserfornewsdail getlinkandidic]valueForKey:@“items”]objectAtIndex:0];
NSURL*imgurl=[NSURL URLWithString:[[Dic valueForKey:@“image”]STRINGBYADINGPERCESSEACEUSINGENCODE:NSUTF8STRINGENCONDING];
NSArray*子视图sarr=[self.view子视图];
对于(int i=0;i=[\u arrUrlLinks count])
返回;
//如有必要,请替换占位符
控制器=[viewControllers objectAtIndex:page];
如果((NSNull*)控制器==[NSNull]){
NSString*deviceType=[UIDevice currentDevice]。型号;
如果([deviceType IsequalString:@“iPhone”])
{
控制器=[[MyViewController alloc]initWithNibName:@“MyViewController”捆绑包:nil];
}
否则{
控制器=[[MyViewController alloc]initWithNibName:@“MyViewController_ipad”捆绑包:nil];
}
[控制器初始化与页码:第页];
//[controller.view addSubview:_txtView];
//[controller.view addSubview:_imageview];
NSLog(@“loadscrollviewwithpage”);
[viewControllers replaceObjectAtIndex:page with Object:controller];
//[控制器释放];
}
//将控制器视图添加到滚动视图
if(nil==controller.view.superview){
CGRect frame=\u scrollView.frame;
frame.origin.x=frame.size.width*页;
frame.origin.y=0;
controller.view.frame=frame;
//_txtView.frame=帧;
//_imageview.frame=frame;
//_txtView.frame=CGRectMake(4.0f、316.0f、310.0f、159.0f);
_txtView=@“你好”;
//[controller.view addSubview:_txtView];
[\u imageView1 addSubview:controller.view];
//[\u txtView addSubview:controller.view];
//[scrollView添加子视图:controller.view];
NSLog(@“loadscrollviewwithpage………..>>>>>>>>”;
//[self._headersettext:[Dic valueForKey:@“header”];
//[自下载数据:resultDic];
//[自设置值正弦接口:Dic];
[自下载下一步:resultDic];
}
}
-(void)unloadScrollViewWithPage:(int)第页{
如果(第<0页)返回;
如果(第>=[[u页链接计数])返回;
控制器=[viewControllers objectAtIndex:page];
如果((NSNull*)控制器!=[NSNull]){
if(nil!=controller.view.superview)
[controller.view removeFromSuperview];
[viewControllers replaceObjectAtIndex:page withObject:[NSNull]];
//[controller.view addSubview:_txtView];
//[controller.view addSubview:_imageview];
NSLog(@“Unloadscrollviewwithpage”);
//[[NSURLCache sharedURLCache]移除所有缓存响应];
}
}
-(无效)scrollViewDidScroll:(UIScrollView*)发送方{
//我们不希望UIPageControl和中的滚动委托之间出现“反馈循环”
//由用户点击页面控件生成的滚动事件触发的更新
//委托方法。当使用页控件时,我们使用布尔值禁用委托逻辑。
如果(已使用页面控制){
//不执行任何操作-滚动是从页面控件启动的,而不是用户拖动的
返回;
}
//当上一页/下一页显示超过50%时,切换指示器
CGFloat pageWidth=\u scrollView.frame.size.width;
int page=floor((_scrollView.contentOffset.x-pageWidth/2)/pageWidth)+1;
pageControl.currentPage=页面;
//NSLog(@“当前页%d”,第页);
//加载可见页面及其两侧的页面(以避免用户开始滚动时闪烁)
[self-unloadScrollViewWithPage:page-2];
[带页面的自加载滚动视图:第1页];
[带页面的自加载滚动视图:第页];
[self-loadScrollViewWithPage:page+1];
[self-UnloadScrollViewWith page:page+2];
计数=页码+1;
//[self-newCountTitleSet];
//一种可能的优化方法是卸载不再可见的视图+控制器
}
//在滚动拖动开始时,重置滚动源自UIPageControl时使用的布尔值
-(无效)ScrollViewWillBeginDraging:(UIScrollView*)scrollViewLoc
{
CGFloat pageWidth=滚动视图loc.frame.size.width;
CGPoint translation=[scrollViewLoc.PangestureRecognitor TranslationView:scrollViewLoc.superview];
int page=floor((scrollViewLoc.contentOffset.x-pageWidth/2)/pageWidth)+1;
如果(translation.x>0)
{
如果(\u strCurrentNewsSelect!=0)
{
如果(第==0页)
{
NSLog(@“正常”);
NSLog(@“ScrollViewWillBeginDraging=0”);
[self-DownLoadDataPrevious:[\u strCurrentNewsSelect.integervalue]-1];
}
}
否则{
如果(第==1页)
{
NSLog(“上一个按钮”);
[自BTN前点击];