初学者在这里。我已经设法从Parse类到表视图获取文本。在同一个类我有另一列我放相关的图像文件,不知怎的,我无法得到图像文件。如果你能帮我解决这个问题,我将很高兴。解析类名称是新闻,我想要的列是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
我的Cell.h
#import <UIKit/UIKit.h>
#import <Parse/Parse.h>
@interface TableCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UILabel *TitleLabel;
@end
我的cell.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
答案 0 :(得分:1)
在
之后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;
}
}];
这可能会让你前进。
答案 1 :(得分:0)
这将获取相关文件并将其放在cell.imageView中,这是一个PFImageView(UIImageView的Parse版本):
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"];
}