0

我正在按照此示例进行分段表视图:https ://parse.com/questions/using-pfquerytableviewcontroller-for-uitableview-sections

添加第一个对象时,我在重新加载表视图时遇到问题。

viewWillAppear我打电话[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]];
}
4

0 回答 0