这里是初学者,伙计们。我已经设法将文本从Parse类获取到表视图。在同一个类中,我有另一个列,我把相关的图像文件放在那里,但不知何故,我无法获得图像文件。如果你能帮我解决这个问题,我会很高兴的。解析类名是News,我想得到的列是imageFile。这是我的密码。
.h文件
#import <UIKit/UIKit.h>
#import <Parse/Parse.h>
#import "TableCell.h"
@interface ViewController : UIViewController <UITableViewDelegate> {
NSArray *events;
}
@property (weak, nonatomic) IBOutlet UITableView *newsTable;
@end
.m文件
#import "ViewController.h"
#import "TableCell.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize newsTable;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self performSelector: @selector(retreiveFromParse)];
}
- (void) retreiveFromParse {
PFQuery *retrieveEvents = [PFQuery queryWithClassName:@"News"];
[retrieveEvents findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
events = [[NSArray alloc] initWithArray:objects];
}
[newsTable reloadData];
}];
}
//*********************Setup table of folder names ************************
//get number of sections in tableview
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
//get number of rows by counting number of folders
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return events.count;
}
//setup cells in tableView
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//setup cell
static NSString *CellIdentifier = @"EventCell";
TableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
PFObject *tempObject = [events objectAtIndex:indexPath.row];
cell.TitleLabel.text = [tempObject objectForKey:@"Event"];
return cell;
}
//user selects folder to add tag to
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"cell tapped");
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
My Cell.h
#import <UIKit/UIKit.h>
#import <Parse/Parse.h>
@interface TableCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UILabel *TitleLabel;
@end
我的手机.m
#import "TableCell.h"
@implementation TableCell
@synthesize TitleLabel;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
之后
PFObject *tempObject = [events objectAtIndex:indexPath.row];
cell.TitleLabel.text = [tempObject objectForKey:@"Event"];
写在下面几行。。
PFFile *imageFile = [tempObject objectForKey:@"image"];
PFImageView *imageView = [[PFImageView alloc] init];
imageView.file = imageFile;
[imageView loadInBackground:^(UIImage *img,NSError *error){
if(!error)
{
UIImageView *yourImageView = [[UIImageView alloc] init];
yourImageView.image = imageView.image;
/*OR*/
cell.imageView.image = imageView.image;
}
}];
这可能会让你继续前进。
这将获取关联的文件并将其放入cell.imageView,这是PFImageView(UIImageView的解析版):
PFObject *tempObject = [events objectAtIndex:indexPath.row];
cell.TitleLabel.text = [tempObject objectForKey:@"Event"];
PFFile *imageFile = [tempObject objectForKey:@"imageFile"];
if (imageFile) {
cell.imageView.file = imageFile;
[cell.imageView loadInBackground];
} else {
cell.imageView.image = [UIImage imageNamed:@"AvatarPlaceholder.png"];
}