[英]Where to Reload UITableView Data after parse query
我無法讓我的UITableView重新正確加載。 我的應用有時工作,而有時它崩潰,因為當tableView嘗試更新時,數據元素之一未正確存儲在數組中。 有人可以指出我合適的地方來重新加載我的tableView嗎。
- (void)viewDidLoad {
[super viewDidLoad];
[self queryForNewBooks];}
-(void)queryForNewBooks{
_bookNameArray = [[NSMutableArray alloc] init];
_authorNameArray = [[NSMutableArray alloc] init];
_isbnNumberArray = [[NSMutableArray alloc] init];
_bookImageData = [[NSMutableArray alloc] init];
PFQuery *query = [PFQuery queryWithClassName:@"BooksForSale"];
[query orderByDescending:@"createdAt"];
[query findObjectsInBackgroundWithBlock:^( NSArray *objects, NSError *error) {
if (objects.count >=1) {
for (PFObject *object in objects) {
PFFile *imageFile = [object objectForKey:@"image"];
[imageFile getDataInBackgroundWithBlock:^(NSData *result, NSError *error) {
if (!error) {
NSData *data = result;
NSLog(@"HEYYYYYY");
if (data == NULL) {
}
else{
[ _bookImageData addObject:data];
//I have tried placing it here [self.tableView reloadData]
// NSLog(@"%@",[_bookImageData objectAtIndex:0]);
}
}
}
];
NSDictionary *bookNameDictionary = object[@"nameOfBook"];
NSDictionary *authorNameDictionary = object[@"Author"];
NSDictionary *bookImageDictionary = object [@"image"];
NSDictionary *isbnNumberDictionary = object [@"isbnNumber"];
NSString *objectID = [object objectId];
if (bookNameDictionary != NULL){
NSLog(@"Yo bro here is the book name %@",bookNameDictionary);
[_bookNameArray addObject:bookNameDictionary];
NSLog(@"number: %@", bookNameDictionary);
}
if (bookNameDictionary == NULL) {
[_bookNameArray addObject:@""];
NSLog(@"Blank space");
}
if (authorNameDictionary != NULL) {
[_authorNameArray addObject:authorNameDictionary];
// [_tableData addObject:ft];
NSLog(@"Author Name : %@",_authorNameArray);
// NSLog(@"the table data is %@",_tableData);
}
if (authorNameDictionary == NULL) {
[_authorNameArray addObject:@""];
NSLog(@"Blank space");
}
if (isbnNumberDictionary != NULL){
NSLog(@"Yo bro here is the isbn %@",isbnNumberDictionary);
[_isbnNumberArray addObject:isbnNumberDictionary];
NSLog(@"number: %@", isbnNumberDictionary);
//[self.tableview reloadData];
}
if (isbnNumberDictionary == NULL) {
[_isbnNumberArray addObject:@""];
NSLog(@"Blank space");
}
/* if (bookImageDictionary !=NULL){
[_bookImageData addObject:bookImageDictionary];
}
if (bookImageDictionary ==NULL){
[_bookImageData addObject:@""];
NSLog(@"Blank Space");
}*/
if (objectID != NULL) {
[_objectIDArray addObject:objectID];
NSLog(@"object id is : %@",objectID);
}
if (objectID ==NULL){
[_objectIDArray addObject:@"blank"];
}
}
}
// code
}];
//I have tried placing it here [self.tableView reloadData]
);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.bookNameArray count];
}
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"TableViewCell";
TableViewCell *cell = (TableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
NSLog(@"Here are the book names, %@",_bookNameArray);
cell.bookNameLabel.text = [_bookNameArray objectAtIndex:indexPath.row];
cell.authorNameLabel.text = [_authorNameArray objectAtIndex:indexPath.row];
if ([_bookImageData objectAtIndex:indexPath.row] != NULL ) {
NSLog(@"it seems to work");
UIImage *image = [UIImage imageWithData: [_bookImageData objectAtIndex:indexPath.row]];
cell.bookImageLabel.image = image;
}
else{
NSLog(@"Error");
}
return cell;
}
更新問題:這是聲明PFimageView的正確方法嗎? 我可以只將UIImageView拖到Xib文件中,然后將其類更改為PFImageView嗎? 將其更改為PFImageView后,是否應該能夠像往常一樣將視圖鏈接到插座?
#import <UIKit/UIKit.h>
#import <Parse/Parse.h>
#import <ParseUI/ParseUI.h>
@interface TableViewCell : UITableViewCell
@property (nonatomic, weak) IBOutlet UILabel *bookNameLabel;
@property (nonatomic, weak) IBOutlet UILabel *authorNameLabel;
@property (nonatomic, weak) IBOutlet PFImageView *bookImageLabel;
@property (nonatomic, weak) IBOutlet UILabel *priceOfBook;
@end
#import <Parse/Parse.h>
#import "TableViewCell.h"
#import <ParseUI/ParseUI.h>
@implementation TableViewCell
@synthesize bookNameLabel =_bookNameLabel;
@synthesize authorNameLabel = _authorNameLabel;
@synthesize bookImageLabel = _bookImageLabel;
- (void)awakeFromNib {
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@結束
使代碼復雜的部分是需要使用查詢返回的對象來獲取圖像。 最簡單的解決方案是忽略從查詢完成處理程序中獲取圖像。
取而代之的是,循環返回的對象,構建數組(這里也有改進的余地,但是暫時只解決問題的崩潰部分)。 在_bookImageData
數組中,不要嘗試保留圖像,而應保留每個對象的PFFile
...
// in the loop of objects, omit getDataInBackground and just do... [_bookImageData addObject:object[@"image"]];
在構建表數據源的循環之后,對所述問題(在何處重新加載表)進行了回答。
for (PFObject *object in objects) { // code to build all of the arrays, omitting getDataInBackground // ... } [self.tableView reloadData];
在表格視圖單元格中,將圖像視圖替換為PFImageView
,因為它可以為您獲取圖像數據(以及其他有用的東西,例如緩存數據)。 您的cellForRowAtIndexPath:
將如下所示...
// bookImageLabel must be a PFImageView // remember, we put the PFFile for each object into the _bookImageData array cell.bookImageLabel.file = _bookImageData[indexPath.row]; [cell.bookImageLabel loadInBackground];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.