您的位置:首页 > 其它

iPhone开发之TableView

2012-06-06 23:23 288 查看
TableView用来展示数据,但有一个特点就是只有一维的,因为屏幕小,显示的范围有限。在一维的表格中又可以分为区域(section)和行(row)。

打开Xcode 4.2,新建一个“Empty Application”,命名为“SimpleTableDemo”,选中项目,新建文件“Cocoa Touch -> UIViewController subclass”,命名为“MyTableViewController”,Subclass of选择“UITableViewController”并选中“with XIB for user interface”。

打开AppDelegate.m文件,修改程序启动时显示view,代码如下:

#import "AppDelegate.h"
#import "MyTableViewController.h"

@implementation AppDelegate

@synthesize window = _window;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
MyTableViewController *mtvc = [[MyTableViewController alloc] initWithStyle:UITableViewStyleGrouped];
[self.window addSubview: mtvc.view];
[self.window makeKeyAndVisible];
return YES;
}
...


这时候运行程序只显示一个空白视图,这是为什么



因为UITableView有两个方法还没有实现numberOfSectionsInTableView(返回区域数量)和tableView:numberOfRowsInSection:(返回某区域内的行数)

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
// Return the number of rows in the section.
return 5;
}


这时再运行就显示表格了,只是这个表格内容是空的



现在添加数据到表格中,我们要添加自定义类型数据,创建一个模型类Article,它有两个属性,title和body,代码如下:

//  Article.h 文件
#import <Foundation/Foundation.h>

@interface Article : NSObject
{
NSString *title;
NSString *body;
}

@property(nonatomic, copy) NSString *title;
@property(nonatomic, copy) NSString *body;

- (id) initWithTitle: (NSString *) newTitle
body: (NSString *) newBody;
@end

//  Article.m 文件

#import "Article.h"

@implementation Article
@synthesize title;
@synthesize body;

- (id) initWithTitle:(NSString *)newTitle body:(NSString *)newBody
{
self = [super init];
if (nil != self) {
self.title = newTitle;
self.body = newBody;
}

return self;
}

- (void) dealloc
{
self.title = nil;
self.body = nil;
[super dealloc];
}

@end


接着我们创建一个数组articleList,数组的类型为Article,并加载数组到表格中

//  MyTableViewController.h 文件

#import <UIKit/UIKit.h>

@interface MyTableViewController : UITableViewController
{
NSMutableArray *articleList;
}

@end

//  MyTableViewController.m 文件

@implementation MyTableViewController
// 在viewDidLoad函数中初始化数组变量articleList
- (void)viewDidLoad
{
[super viewDidLoad];
articleList = [[NSMutableArray alloc] init];
Article *article_1 = [[Article alloc] initWithTitle:@"demo1 title" body:@"description demo1"];
[articleList addObject:article_1];
Article *article_2 = [[Article alloc] initWithTitle:@"haha title" body:@"description haha"];
[articleList addObject:article_2];
[article_1 release];
[article_2 release];
}

// 返回行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [articleList count];
}

// 配置单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
//重用单元,当单元滚动出屏幕再出现在屏幕上时
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
//这里可以设置单元风格
//cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}

// Configure the cell...
Article *article = [articleList objectAtIndex:indexPath.row];
[cell.textLabel setText:article.title];

return cell;
}

@end


运行效果如下:





UITableView还有一个横扫表格行,显示删除按钮,用来删除行



只要实现tableView:commitEditingStyle:forRowAtIndexPath方法,其实XCode帮我们生成了一个模板代码,我们只要把注释去掉就行了

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}


这个时候点击”Delete”按钮,程序不能正常删除,原因在于我们在tableView:numberOfRowsInSection方法中返回[articleList count],视图上是删除了,行数上没有删除,计算时出错了,正确的处理应该先删除数据源里面的数据,再删除表格行,代码如下:

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[articleList removeObjectAtIndex: indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}


完整代码

下载

转自:http://wangliang.me/?p=315


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: