使用plist显示自定义TableView单元格?

时间:2014-07-08 14:32:59

标签: ios xcode plist tableview

我想在Table View单元格中显示一个小图像和日期,用户可以单击该单元格以显示包含更多信息的另一个视图。所有这些数据都存储在plist中(图像,日期和其他数字)。

我似乎无法:

  1. 获取要在单元格内显示的图像 - 我将所有图像添加到Supporting Files文件夹中,并尝试将文件名输入到它不接受的plist中。

  2. 添加用户点击单元格的功能,并查看plist数据中的更多信息。

1 个答案:

答案 0 :(得分:1)

首先使用包含字典数组的plist构建“数据库”。这应该是这样的:

enter image description here

创建并NSArray保留此plist:

@property (strong, nonatomic) NSArray *imagesList;

viewDidLoad中加载(假设名为“ImagesList”的plist):

NSString *path = [[NSBundle mainBundle] pathForResource:@"ImagesList" ofType:@"plist"];
self.imagesList = [[NSArray alloc] initWithContentsOfFile:path];

其余的是一些简单的UITableViewDatasource

#pragma mark - UITableViewDatasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.imagesList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];

    if(!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:@"myCell"];
    }

    NSDictionary *eachImage = [self.imagesList objectAtIndex:indexPath.row];

    cell.textLabel.text = [eachImage objectForKey:@"date"];
    cell.detailTextLabel.text = [eachImage objectForKey:@"additionalDetail"];

    cell.imageView.image = [UIImage imageNamed:[eachImage objectForKey:@"imageName"]];

    return cell;
}

不要忘记将图片放入应用包中并正确重命名。

相关问题