在工程中新建一个controller类,命名为bookselfViewController,则该类的声明头文件内容如下:
bookselfViewController.h #import <UIKit/UIKit.h> @interface bookselfViewController : UIViewController <UITableViewDelegate,UITableViewDataSource>{ NSArray *listData; UITableView *theTableView; int rowNum; } -(void)numberOfRow; @property (nonatomic, retain) NSArray *listData; @property (nonatomic, retain) UITableView *theTableView; @end
该类实现内容如下:
bookselfViewController.m #import "bookselfViewController.h" @implementation bookselfViewController @synthesize listData; @synthesize theTableView; /********************************************************************************* 函数: - (void)viewDidLoad 功能: 启动程序加载首界面,并初始化应用程序的数据源信息 参数: 返回值: **********************************************************************************/ - (void)viewDidLoad { NSArray *array = [[[NSArray alloc] initWithObjects:@"baobaoweiyang.png",@"baojian.png",@"xinlingjiehuo.png",@"zhunbaba.png",@"zhunmama.png",nil] autorelease]; self.listData = array; [self init]; [super viewDidLoad]; }
/********************************************************************************* 函数: - (id)init 功能: 创建视图,创建和添加tableview视图,并进行屏幕滚动控制处理 参数: 返回值:所创建对象本身 **********************************************************************************/ - (id)init { if (self = [super init]) { self.view = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]; // Setup the background UIImageView *background = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"backgroud.png"]]; background.frame = CGRectMake(0, 0, 320, 480); [self.view addSubview:background]; [background release]; // Create table view theTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 460) style: UITableViewStylePlain]; [theTableView setDelegate:self]; [theTableView setDataSource:self]; // This should be set to work with the image height [theTableView setRowHeight:115]; // Transparent, so we can see the background [theTableView setBackgroundColor:[UIColor clearColor]]; [theTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone]; [theTableView setIndicatorStyle:UIScrollViewIndicatorStyleWhite]; [self numberOfRow]; //NSLog(@"%d",rowNum); if(rowNum*115 <= 480) theTableView.scrollEnabled = FALSE; else theTableView.scrollEnabled =TRUE; [self.view addSubview:theTableView]; } return self; }