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

标签 ios xcode plist tableview

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

我似乎不能:

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

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

最佳答案

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

enter image description here

创建和 NSArray 来保存这个 plist:

@property (strong, nonatomic) NSArray *imagesList;

然后在 viewDidLoad 中加载(假设 plist 名为“ImagesList”):

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;
}

不要忘记将您的图像放入应用程序包中并正确重命名。

关于ios - 使用 plist 显示自定义 TableView 单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24634549/

相关文章:

ios - 无法在 IIS 7 中的 iOS 7 上部署应用程序

ios - 如何在不调用的情况下从另一个应用程序中打开电话应用程序?

ios - 如何 swift 从 parent 那里解雇一个弹出窗口

ios - UITableView:当 gestureRecognizers 激活时,scrollViewDidScroll 执行并且 contentOffset 跳转

ios - Swift:使用 for in 循环从 plist 中检索数据

xml - 解析大型 PLIST 和内存占用

ios - iOS 是否支持可跨越的字符串?

ios - iOS SDK 中的 MDM 配置文件安装失败

ios - 如何在 iPhone 上验证 url

xcode - 如何在 XCode 4.2 中重命名 NSArrayController?