I want to display a small image and date inside of a Table View cell, which the user can click on to display another view with more information. All of this data is stored in a plist (image, date, and other numbers).
I can't seem to:
Get the images to display inside of the cells - I added all of my images into the Supporting Files folder and tried to enter the filenames into the plist which it did not accept.
Add the functionality for the user to click on a cell and view more information from the plist data.
First you construct your "database" using plist which contains an array of dictionary. This should be something like this:
Create and NSArray
to hold this plist:
@property (strong, nonatomic) NSArray *imagesList;
And do the loading in viewDidLoad
(suppose that the plist named "ImagesList"):
NSString *path = [[NSBundle mainBundle] pathForResource:@"ImagesList" ofType:@"plist"];
self.imagesList = [[NSArray alloc] initWithContentsOfFile:path];
The rest is some simple 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;
}
Don't forget to put your images into app bundle and rename it properly.