iOS开发笔记(二)Table View

373 查看

Table View

Table View 在iOS开发中是一个很重要的控件。

从Object Library中可以找到Table View控件。

在建立的项目文件目录选择Main.Storyboard切换到Storyborad界面,然后从Object Library从拖拽Table View控件到View中。

在Table View中显示数据

将文件SimpleTableViewController.h修改代码如下:

@interface SimpleTableViewController:UIViewController <UITableViewDelegate, UITableViewDataSource>

PS.我这里项目Class prefix是SimpleTable。
然后在SimpleTableViewController.m中定义一个instance variable 用来存储将要在Table View 中显示的数据。

@implementation SimpleTableViewController{
    NSArray *dataList;
}

并且在viewDidLoad方法中初始化和赋值。

-(void)viewDidLoad
{
    [super viewDidLoad];
    dataList = [NSArray arrayWithObjects:@"item1",@"item2",@"item3",nil];
}

现在我们添加两个方法:tableView:numberOfRowsInSectiontableView:cellForRowAtIndexPath。这两个方法是UITableViewDataSource协议中的一部分。

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *identifier = @"SimpleTableIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if(cell == nil){
        cell = [UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.textLabel.text = [dataList objectAtIndex:indexPath.row];
    return cell;
}

现在还需要在Table ViewDataSourceDelegate之间建立connection。
切换到Storyboard界面中选择Table View并右键,得到如下:

可以看到Outlets中有dataSource和delegate两个项目,现在需要将这两个项目和Simple Table View Controller之间建立connection。方法是按住dataSource和delegate右侧的圆圈然后拖拽到Simple Table View Controller上,如下图所示:

最终得到如下:

然后运行

添加缩略图
「Add Files to "SimpleTable"...」
然后添加一张图片。我这里添加的是wallpaper-1791868.jpg

然后在方法tableView:cellForRowAtIndexPath:中添加以下代码:

cell.imageView.image = [UIImage imageNamed:@"wallpaper-1791868.jpg"];

然后运行查看效果。

从Property list中读取并显示数据

上面直接将数据直接保存在代码中,在实际开发中好的实践应该是代码和数据分离,将数据保持到数据库或者本地文件中。所以这里要提到Property list文件。

新建一个Property list文件

编辑Property list文件

然后我们来读取Property list文件中的数据

NSString *path = [NSBundle mainBundle] pathForResource:@"recipes" ofType:@"plist"];
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
tableData = [dict objectForKey:@"Recipes Name"];
thumbnail = [dict objectForKey:@"Thumbnail"];
prepTime = [dict objectForKey:@"prepTime"];

修改tableView:numberOfRowsInSection:tableView:cellForRowAtIndexPath:方法的代码。

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *simpleTableIdentifier = @"SimpleTableIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if(cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }
    cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
    cell.imageView.image = [UIImage imageNamed:[thumbnail objectAtIndex:indexPath.row]];
    return cell;
}

运行,可以发现读取数据成功。