Elton's Blog

iPhone Table View 教程(2) 创建一个简单表格

by on 七.05, 2009, under iPhone

上一节我们介绍了iPhone的table view,这节我们先做一个简单的表格例子。

在Xcode中新建一个view based项目,命名为SimpleTable。

新建xcode项目

新建xcode项目

在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

File‘s Owner

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。

设置data source和delegate

设置data source和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

运行可以得到:

运行结果

运行结果





:, ,

14 Comments for this entry

  • blood stool

    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.

  • finance personal software

    After reading you site, Your site is very useful for me .I bookmarked your site!

  • accountmoney

    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.

  • zuo

    非常好,很受用,清楚明了

  • Edmond Sims

    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.

  • Collin Pfefferkorn

    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.

  • Christian Louboutin

    Thanks for posting! I really enjoyed the report. I’ve already bookmark

    this article.

  • wholesale sunglasses

    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?

  • Louboutin shoes

    Great article, thank you very much!

  • wholesale cellular phones

    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.

  • Bryan Rhone

    My cousin recommended this blog and she was totally right keep up the fantastic work!

  • youminbuluo

    额,我发现官方的TableViewSuite例子里面没有datasource和delegate协议(它没有用interface builder,而是直接用代码写的,但是声明的@interface RootViewController : UITableViewController {
    ………………
    }后面没有带上2个协议
    )

    不过好像默认这两个协议还是存在的,因为下文的
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 显然是协议里面规定的东西

Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit my friends!

A few highly recommended friends...