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

iPhone开发乱乱记

阅读更多

自由布局,UIButton样式,下阴影,按钮事件。

导航栏rightBarButtonItem增加UISegmentedControl控件。

 

 

 

 

#import <QuartzCore/QuartzCore.h>
#import "LeagueController.h"
#import "WaitDialog.h"
#import "JSONParser.h"
#import "FunUtil.h"

@implementation LeagueController
@synthesize selectDate;
@synthesize flag;
@synthesize topToolBar;
@synthesize mainView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}
-(id)initWithDate:(CFGregorianDate) date{
	self = [super initWithNibName:nil bundle:nil];
	if(self){
		self.selectDate = date;
		self.title = @"联赛选择";
	
		NSArray *buttonNames = [NSArray arrayWithObjects:@"所有", @"一级", @"二级", nil];
		topToolBar = [[UISegmentedControl alloc] initWithItems:buttonNames];
		[topToolBar setFrame:CGRectMake(1, 1, 120, 30)];
		topToolBar.selectedSegmentIndex = 0;
		topToolBar.segmentedControlStyle = UISegmentedControlStyleBar;
		[topToolBar addTarget:self action:@selector(selectFlag:) forControlEvents:UIControlEventValueChanged];
		UIBarButtonItem *segButton = [[UIBarButtonItem alloc] initWithCustomView:topToolBar];
		self.navigationItem.rightBarButtonItem = segButton; 
		
		self.mainView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
		[self.view addSubview:mainView];
		[segButton release];
		self.flag = 0;
	}
	return self;
}
-(void)removeAllView{
	for(UIView *subView in [self.mainView subviews]){
		[subView removeFromSuperview];
	}
}
- (void)viewDidAppear:(BOOL)animated{
	[super viewDidAppear:animated];
	self.mainView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
	[[WaitDialog sharedWaitDialog] setLoadingLabel:@"加截数据..."];
	[NSThread detachNewThreadSelector:@selector(fetchData) toTarget:self withObject:nil];
}
-(void)selectFlag:(UISegmentedControl *)Seg{
	int index = Seg.selectedSegmentIndex;
	self.flag = index;
	[NSThread detachNewThreadSelector:@selector(fetchData) toTarget:self withObject:nil];
}
-(void) fetchData{
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
	NSString *date = [NSString stringWithFormat:@"%d-%d-%d",selectDate.year,selectDate.month,selectDate.day];
	NSString *dataURL =@"";
	NSArray *data = [JSONParser loadData:dataURL isAllValues:NO valueForKey:@"list"];
	[self performSelectorOnMainThread:@selector(makeView:) withObject:data waitUntilDone:NO];
	[pool release];
}
-(void)makeView:(NSArray*)data{
	[self removeAllView];
	[[WaitDialog sharedWaitDialog]endShowLoading];
	if(data == nil || [data 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 boldSystemFontOfSize:20];
		[self.mainView addSubview:nullData];
	}else{
		int count = [data count];
		int i = 0;
		int x = 10;
		int y = 10;
		int cols = 3;
		int width = self.view.frame.size.width/cols -20;
		int height = 30;
		int contentHeight = (height+y*2)*(count/cols);
		
		int c = count/cols+1;
		for(i = 0;i<c;i++){
			NSArray *league = [data objectAtIndex:i];
			UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
			btn.layer.cornerRadius = 5;
			btn.layer.shadowOffset =  CGSizeMake(3, 5);
			btn.layer.shadowOpacity = 0.8;
			btn.layer.shadowColor =  [UIColor blackColor].CGColor;
			btn.frame = CGRectMake(x, y, width, height);
			btn.backgroundColor =  [FunUtil colorWithHexString:[league valueForKey:@"color"]];
			[btn setTitleColor:[UIColor whiteColor ]forState:UIControlStateNormal];
			[btn setTitle:[league valueForKey:@"cnName"] forState:UIControlStateNormal];
			[btn setTag:[[league valueForKey:@"lid"] intValue]];
			[btn addTarget:self action:@selector(selectLeague:) forControlEvents:UIControlEventTouchUpInside];
			[btn addTarget:self action:@selector(buttonDown:) forControlEvents:UIControlEventTouchDown];
			[self.mainView addSubview:btn];
			y+=height+10;
		}
		
		contentHeight = (height+10)*c+10;
		CGSize newSize = CGSizeMake(self.view.frame.size.width, contentHeight);
		[self.mainView setContentSize:newSize];
	}
}
-(void)selectLeague:(UIButton*)sender{
	[sender setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
	sender.layer.shadowOffset =  CGSizeMake(3, 5);
	sender.layer.shadowOpacity = 0.8;
	sender.layer.shadowColor =  [UIColor blackColor].CGColor;
}
-(void)buttonDown:(UIButton*)sender{
	[sender setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
	sender.layer.shadowOffset =  CGSizeMake(0,0);
	sender.layer.shadowOpacity = 1;
	sender.layer.shadowColor =  [UIColor blackColor].CGColor;
}
#pragma mark TableViewDataSource Methods

- (void)dealloc {
	[topToolBar release];
	[mainView release];
    [super dealloc];
}


@end

 

  • 大小: 242.6 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics