我想在 TableView 单元格内显示一个小图像和日期,用户可以单击它以显示包含更多信息的另一个 View 。所有这些数据都存储在一个 plist 中(图像、日期和其他数字)。
我似乎不能:
让图像显示在单元格内 - 我将所有图像添加到 Supporting Files 文件夹中,并尝试将文件名输入到它不接受的 plist 中。
为用户添加点击单元格并从 plist 数据中查看更多信息的功能。
最佳答案
首先,您使用包含字典数组的 plist 构建您的“数据库”。这应该是这样的:
创建和 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/