`
gekie
  • 浏览: 155286 次
  • 性别: Icon_minigender_1
  • 来自: 海口
社区版块
存档分类
最新评论

代码自定义UIScrollView实现

阅读更多

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
		self.title = @"主流公司指数";
		mainView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
		mainView.delegate = self;
		
		[self.view addSubview:mainView];
		UIBarButtonItem *reloadItem = [[UIBarButtonItem alloc] initWithTitle:@"重新加载" style:UIBarButtonItemStylePlain target:self action:@selector(reloadItem_click)];	
		
		self.navigationItem.rightBarButtonItem = reloadItem;
		
		/*mainView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width, 400)];
		mainView.directionalLockEnabled = YES;
		mainView.pagingEnabled = YES;
		mainView.backgroundColor = [UIColor blueColor];
		mainView.showsVerticalScrollIndicator = NO;
		mainView.showsHorizontalScrollIndicator = NO;
		mainView.delegate = self;
		
		CGSize newSize = CGSizeMake(self.view.frame.size.width * 2,  self.view.frame.size.height);
		[mainView setContentSize:newSize];
		
		[self.view addSubview:mainView];
		
		pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0.0, 401, self.view.frame.size.width, 80)];
		pageControl.hidesForSinglePage = YES;
		pageControl.userInteractionEnabled = NO;
		pageControl.backgroundColor = [UIColor redColor];
		[self.view addSubview:pageControl];
		
		page1=[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width, self.view.frame.size.height)];
		page1.backgroundColor=[UIColor redColor];
		
		[mainView addSubview:page1];
		
		
		page2=[[UIView alloc] initWithFrame:CGRectMake(self.view.frame.size.width, 0.0, self.view.frame.size.width, self.view.frame.size.height)];
		page2.backgroundColor=[UIColor blueColor];
		
		[mainView addSubview:page2];		//[mainView setBackgroundColor:[UIColor redColor]];
		 */
		
	}
    return self;
}


/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	
	/*BfViewController *bf = [[BfViewController alloc]init];
	[self.view addSubview:bf.view];
	[bf setTopicText:@"test"];
	[bf setMarkText:@"mark"];
	 */
	/*[super viewDidLoad];
	waitView = [[UIWaitingView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
	[self.view addSubview:waitView];
	[self.view bringSubviewToFront:waitView];
	[waitView starActivityLoading];
	
	[waitView setLoadingLabel:@"正在加载数据..."];
	 */
	
	//[bf release];
	//[bf2 release];
}
- (void)viewDidAppear:(BOOL)animated{
	NSLog(@"CompanyController.viewDidAppear");
	[super viewDidAppear:animated];
	if(companyList==nil){
		[[WaitDialog sharedWaitDialog] setLoadingLabel:@"链接网络中.."];
		timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(fetchData) userInfo:nil repeats:NO];
	}
}
-(void)fetchData{
	[self removeAllView];
	companyList = [JSONParser loadData:@"http://3g.wapzq.com/odds/json/company.jsp" isAllValues:NO valueForKey:@"list"];
	//NSLog(@"companyList:%@",arr);
	//[waitView endActivityLoading];
	//[waitView removeFromSuperview];
	//[[WaitDialog sharedWaitDialog]endShowLoading];
	[[WaitDialog sharedWaitDialog]setLoadingLabel:@"正在构造界面..."];
	[self makeView];
}
-(void)removeAllView{
	for(UIView *subView in [mainView subviews]){
		[subView removeFromSuperview];
	}	
}
-(void)makeView{
	[[WaitDialog sharedWaitDialog]endShowLoading];

	if(companyList==nil||[companyList count]==0){
		UILabel *nullData = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, self.view.frame.size.width-20, 40)];
		nullData.text = @"暂时没有数据";
		nullData.backgroundColor = [UIColor clearColor];
		nullData.font = [UIFont systemFontOfSize:20];
		[mainView addSubview:nullData];
	}else{
		int count = [companyList count];
		int i = 0;
		int x = 10;
		int y = 10;
		int width = self.view.frame.size.width / 2 - 40;
		int height = 30;
		int contentHeight = (height+y*2)*count/2;
		CGSize newSize = CGSizeMake(self.view.frame.size.width,contentHeight);
		[mainView setContentSize:newSize];
		for(i = 0; i < count/2; i++){
			NSArray *company = [companyList objectAtIndex:i];
			// NSLog(@"index %d has %@.",i, [companyList objectAtIndex:i]);
			//NSLog(@"company.id:%@,name:%@",[company valueForKey:@"cid"],[company valueForKey:@"name"]);
			UIButton *btn = [[UIButton buttonWithType:UIButtonTypeRoundedRect]retain];
			btn.frame = CGRectMake(x, y, width, height);
			btn.backgroundColor = [UIColor clearColor];
			//UIButton*btn=[[UIButton alloc]initWithFrame:CGRectMake(x, y, width, height)];
			//btn.backgroundColor =  [UIColor colorWithRed:0.52 green:0.09 blue:0.07 alpha:1];
			//[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
			[btn setTitle:[company valueForKey:@"name"] forState:UIControlStateNormal];
			//btn.titleLabel.font = [UIFont systemFontOfSize:19];
			
			[btn addTarget:self action:@selector(selectCompany:) forControlEvents:UIControlEventTouchUpInside];
			[mainView addSubview:btn];
			[btn release];
			y+=height+10;
			
			
		}
		x = self.view.frame.size.width /2 +10;
		y = 10;
		for(i;i<count;i++){
			NSArray *company = [companyList objectAtIndex:i];
			UIButton *btn = [[UIButton buttonWithType:UIButtonTypeRoundedRect]retain];
			btn.frame = CGRectMake(x, y, width, height);
			//[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
			[btn setTitle:[company valueForKey:@"name"] forState:UIControlStateNormal];
			//btn.titleLabel.font = [UIFont systemFontOfSize:19];
			[btn addTarget:self action:@selector(selectCompany:) forControlEvents:UIControlEventTouchUpInside];
			[mainView addSubview:btn];
			[btn release];
			y+=height+10;
			
		}
		
	}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics