ITEEDU

界面控件布局及UITableView实例化

在该项目中,有一部分代码也是经过本人写的,是有关iPhone界面控件布局和UITableView的一个简单例子。

头文件:

@interface ZhengWuViewController_iphone : UIViewController <UITableViewDataSource,UITableViewDelegate>{

	NSArray *listData;
	
}
@property(nonatomic,retain)NSArray *listData;
@end
类实现文件:
//  ZhengWuViewController_iphone.m

#import "ZhengWuViewController_iphone.h"
#import "DetailViewController.h"

@implementation ZhengWuViewController_iphone
//@synthesize listOfItems;
@synthesize listData;

// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
	[super loadView];
	
	NSArray *array = [NSArray arrayWithObjects:@"  海关法规",@"  海关总署令",@"  海关总署公告",@"  政策解读",@"  海关统计",nil];
	self.listData = array;
	[array release];
	
	UITableView *tempTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-88) 
														  style:UITableViewStyleGrouped];
	tempTable.delegate = self;
	tempTable.dataSource = self;
	[self.view addSubview:tempTable];
	[tempTable release];
	
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


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

#pragma mark -
#pragma mark Table Data Source Methods


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
		return [self.listData count];
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	static NSString *cellIndentifier = @"zhengwucell";
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
	//cell = [tableView cellForRowAtIndexPath:indexPath];
	if (cell == nil) {
		cell =[[[UITableViewCell alloc] initWithFrame:CGRectZero
											  reuseIdentifier:cellIndentifier] autorelease];
	}
	
	NSUInteger row = [indexPath row];
	cell.textLabel.text = [listData objectAtIndex:row];
	
	return cell;
}
#pragma mark -
#pragma mark Table Delegate Methods


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	NSString *url ;
	switch (indexPath.row) {
		case 0:
			url = @"http://219.237.194.238:18000/httpecd_csswww.customs.gov.cn/default.aspx?tabid=3383";
			break;
		case 1://公开目录
			url = @"http://219.237.194.238:18000/httpecd_cssgkml.customs.gov.cn/tabid/106/Default.aspx";
			break;
		case 2://政府采购
			url = @"http://219.237.194.238:18000/httpecd_csswww.customs.gov.cn/Default.aspx?tabid=9204";
			break;
		case 3: //拍卖信息
			url = @"http://219.237.194.238:18000/httpecd_csswww.customs.gov.cn/tabid/4376/MoreModuleID/92745/MoreTabID/39114/Default.aspx";
			break;
		case 4: //政策解读
			url = @"http://219.237.194.238:18000/httpecd_csswww.customs.gov.cn/Default.aspx?tabid=7985";
		    break;
		default:
			break;
	}	if (url != nil) {
		DetailViewController *detailView = [[[DetailViewController alloc] init] autorelease];
		detailView.urlString = url;
		[self.navigationController pushViewController:detailView animated:YES];
		//detailView.hidesBottomBarWhenPushed = YES;	
	}

}
@end
2.UIButton上的图片和文字布局

- (void)loadView {
	[super loadView];
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
		ipad = TRUE; //ipad需要重新进行设置
	}
      UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(5, 10, 200, 25)] autorelease];
	if (ipad) {
		label.frame = CGRectMake(15, 30, 200, 25);
	}
	label.backgroundColor = [UIColor clearColor];
	label.textColor = [UIColor blueColor];
	label.text = @"办事服务";
	[self.view addSubview:label];
    
    UIView *view1 = [self buttonView:CGRectMake(50, 50, 75, 75) labelString:@"通关状态查询"      imageName:@"tongguan" tag:101];
	if (ipad) {
		view1.frame = CGRectMake(87, 80, 95, 95);
	}
	[self.view addSubview:view1];
	[self.view bringSubviewToFront:view1];

	UIView *view2 = [self buttonView:CGRectMake(162, 50, 75, 75) labelString:@"商品信息查询"  imageName:@"xinxi_02" tag:102];
	if (ipad) {
		view2.frame = CGRectMake(256, 80, 95, 95);
	}
	[self.view addSubview:view2];
	[self.view bringSubviewToFront:view2];
 
}
//buttonView函数
-(UIView *)buttonView:(CGRect )rect labelString:(NSString *)str imageName:(NSString*)name tag:(NSInteger)num
{
	UIButton *bt = [[[UIButton alloc] init] autorelease];
	if (ipad) {
		bt.frame = CGRectMake(5, 5, 114, 114);
	}else {
		bt.frame = CGRectMake(2, 2, 92, 92);
	}
    
	bt.tag = num;
	[bt addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];	
	UILabel *label = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
	if (ipad) {
		label.frame = CGRectMake(10, 75 , 100, 25);
	}else {
		label.frame = CGRectMake(8, 70, 80, 20);
	}
	label.backgroundColor = [UIColor clearColor];
	label.textColor = [UIColor blueColor];
	label.text = str;
	label.font = [UIFont fontWithName:@"Arial" size:12];
	label.textAlignment = UITextAlignmentCenter;
        UIImage *image;
        image =[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:name ofType:@"png"]];
 
	UIImageView *iview = [[[UIImageView alloc] initWithImage:image] autorelease];
	if (ipad) {
		iview.frame = CGRectMake(17, 10, 65, 65);
	}else {
		iview.frame = CGRectMake(20, 10, 54, 54);
	}
	
	UIView *view = [[[UIView alloc] initWithFrame:rect] autorelease];
	[bt addSubview:iview];
	[bt sendSubviewToBack:iview];
	[bt addSubview:label];
	[bt sendSubviewToBack:label];
	UIImage *bgimage =[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"sbg" ofType:@"png"]];
	UIImageView *bgview = [[[UIImageView alloc] initWithImage:bgimage] autorelease];
	if (ipad) {
		bgview.frame = CGRectMake(0, 0, 116, 116);
	}else {
		bgview.frame = CGRectMake(0, 0, 94, 94);
	}
	[bt setBackgroundImage:bgimage forState:UIControlEventTouchDown];
	[view addSubview:bt];
	[view bringSubviewToFront:bt];
	return view;
}