セクション化されたテーブルビューのこの例に従っています: https://parse.com/questions/using-pfquerytableviewcontroller-for-uitableview-sections
最初のオブジェクトが追加されたときにテーブル ビューをリロードするのに問題があります。
viewWillAppear
I callとI call[self loadObjects]
の最後に、テーブルがまだ更新されていません。アプリを強制終了して再起動すると、完全に機能します。objectsDidLoad
[self.tableView reloadData]
アプリの初回使用時にテーブルが更新されるようにするには、どうすればよいですか?
編集:
これが私のコードです:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// The className to query on
self.parseClassName = @"TheClassName";
// Whether the built-in pull-to-refresh is enabled
self.pullToRefreshEnabled = YES;
// Whether the built-in pagination is enabled
self.paginationEnabled = NO;
self.shouldReloadOnAppear = NO;
self.sections = [NSMutableDictionary dictionary];
self.sectionToFinanceTypeMap = [NSMutableDictionary dictionary];
}
return self;
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self loadObjects];
[self.tableView reloadData];
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.sections.allKeys.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSString *financeType = [self financeTypeForSection:section];
NSArray *rowIndecesInSection = [self.sections objectForKey:financeType];
return rowIndecesInSection.count;
}
#pragma mark - PFQueryTableViewController
- (PFQuery *)queryForTable {
PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];
[query whereKey:@"user" equalTo:[PFUser currentUser]];
[query orderByDescending:@"financeType"];
// A pull-to-refresh should always trigger a network request.
[query setCachePolicy:kPFCachePolicyNetworkOnly];
// If no objects are loaded in memory, we look to the cache first to fill the table
// and then subsequently do a query against the network.
//
// If there is no network connection, we will hit the cache first.
if (self.sections.allKeys.count == 0 || ![[UIApplication sharedApplication].delegate performSelector:@selector(isParseReachable)]) {
[query setCachePolicy:kPFCachePolicyCacheThenNetwork];
}
return query;
}
- (void)objectsDidLoad:(nullable NSError *)error {
[super objectsDidLoad:error];
// This method is called every time objects are loaded from Parse via the PFQuery
[self.sections removeAllObjects];
[self.sectionToFinanceTypeMap removeAllObjects];
NSInteger section = 0;
NSInteger rowIndex = 0;
for (PFObject *object in self.objects) {
NSString *financeType = [object objectForKey:@"financeType"];
NSMutableArray *objectsInSection = [self.sections objectForKey:financeType];
if (!objectsInSection) {
objectsInSection = [NSMutableArray array];
// this is the first time we see this financeType - increment the section index
[self.sectionToFinanceTypeMap setObject:financeType forKey:[NSNumber numberWithInteger:section++]];
}
[objectsInSection addObject:[NSNumber numberWithInteger:rowIndex++]];
[self.sections setObject:objectsInSection forKey:financeType];
}
[self.tableView reloadData];
}
- (PFObject *)objectAtIndexPath:(NSIndexPath *)indexPath {
NSString *sportType = [self financeTypeForSection:indexPath.section];
NSArray *rowIndecesInSection = [self.sections objectForKey:financeType];
NSNumber *rowIndex = [rowIndecesInSection objectAtIndex:indexPath.row];
return [self.objects objectAtIndex:[rowIndex intValue]];
}