iPhone Table View 教程(2) 创建一个简单表格
by Elton on 七.05, 2009, under iPhone
上一节我们介绍了iPhone的table view,这节我们先做一个简单的表格例子。
在Xcode中新建一个view based项目,命名为SimpleTable。
在SimpleTableViewController.h输入以下代码:
1 2 3 4 5 6 7 | #import @interface SimpleTableViewController : UIViewController { IBOutlet UITableView *tblSimpleTable; NSArray *arryData; } @end |
定义一个table view IBOutlet变量和一个数组。
打开SimpleTableViewController.xib文件,进入interface builder。
从Library拖入一个table view控件到主窗口中。
选择File’s Owner
按cmd + 2 打开 ‘Connections Inspector’
点击tblSimpleTable边上的小圆圈,拖拽到主屏幕上的table view上,将IBOutlet变量跟Interface Builder中的表格table view关联起来。
在选择主屏幕中的table view,拖拽dataSource和delegate两个变量拖拽到File’s Owner上,指定File‘s Owner所对应的对象(即SimpleTableViewController)为tableview的datasource和delegate。
打开SimpleTableViewController.m,编辑其中内容为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | #import "SimpleTableViewController.h" @implementation SimpleTableViewController // Implement viewDidLoad to do additional setup after loading the view, typically from a nib. - (void)viewDidLoad { arryData = [[NSArray alloc] initWithObjects:@"iPhone",@"Android",@"Plam Pre",@"Windows Mobile",nil]; [super viewDidLoad]; } - (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 { // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; } - (void)dealloc { [arryData release]; [super dealloc]; } #pragma mark Table view methods - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } // Customize the number of rows in the table view. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [arryData count]; } // Customize the appearance of table view cells. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; } // Set up the cell... cell.textLabel.text = [arryData objectAtIndex:indexPath.row]; return cell; } @end |
运行可以得到:








二月 17th, 2010 on 04:26
Saw your site bookmarked on Reddit.I love your site and marketing strategy.Your site is very useful for me .I bookmarked your site!
I am been engaged 5 years on the blood stool If you have some questions, please get in touch with me.
二月 17th, 2010 on 09:30
Thank you.
二月 26th, 2010 on 18:12
After reading you site, Your site is very useful for me .I bookmarked your site!
二月 27th, 2010 on 13:04
Hello,my name is andy! i come from Los Angeles!After reading your this forum, I thought your forum is great! I am very like your forum and I am very interested in the field of this forum thread. I hope also wrote some thread in this forum, if you have time, welcome to make friends Whit me, I am very appreciated that you can share your idea with me. Acturally, I am still a student, and I am bury myself in health study. This forum is very useful for me .I just bookmarked it! i love life and people and drugs and music! surely we’ll have something in common.
五月 13th, 2010 on 01:14
非常好,很受用,清楚明了
五月 25th, 2010 on 10:22
I’m new on here, I hit upon this website I find It really helpful and its helped me out a lot. I will be able to contribute & aid other users like it has helped me.
六月 11th, 2010 on 11:32
Intimately, the post is actually the freshest on this deserving topic. I harmonize with your conclusions and will eagerly look forward to your next updates. Saying thanks will not just be enough, for the extraordinary clarity in your writing. I will at once grab your rss feed to stay abreast of any updates.Solid work and much success in your business enterprise!Thanks.
六月 13th, 2010 on 05:59
Thanks for posting! I really enjoyed the report. I’ve already bookmark
this article.
六月 15th, 2010 on 08:34
I agree with your Blog and I will be back to check it more in the future so please keep up your work. I love your content & the way that you write. It looks like you’ve been doing this for a while now, how long have you been blogging for?
六月 17th, 2010 on 05:57
Great article, thank you very much!
六月 22nd, 2010 on 06:24
This is really a incredibly excellent read for me, Have to admit you are 1 with the very best bloggers I ever saw.Thanks for posting this informative article.
六月 27th, 2010 on 19:58
My cousin recommended this blog and she was totally right keep up the fantastic work!
七月 19th, 2010 on 16:59
额,我发现官方的TableViewSuite例子里面没有datasource和delegate协议(它没有用interface builder,而是直接用代码写的,但是声明的@interface RootViewController : UITableViewController {
………………
}后面没有带上2个协议
)
不过好像默认这两个协议还是存在的,因为下文的
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 显然是协议里面规定的东西
十月 14th, 2010 on 15:08
Thanks for this post.