我关注此论坛:https://parse.com/questions/using-pfquerytableviewcontroller-for-uitableview-sections
如果您转到上面的链接并阅读标题,您会看到我正在尝试在PFQueryTableViewController
中创建部分。从上面的链接复制代码后,我能够对我的TableView进行切割就好了......它非常棒!这是我的问题。 (这样做的最好方法是举个例子)。想象一下,我有3节细胞,每节有2个细胞
一个 乙
Ç d
电子 ˚F
当我点击A时,我得到一个结果。当我点击B时,我得到B结果。 但是当我点击C时我得到A,当我点击D时我得到B. 当我点击E时,我得到A,当我点击F时,我得到B。
基本上它知道有部分,但它正好像有1个部分不断重复(3)次。
这是捕获。 TableView中的单元格显示正确的信息。在您单击单元格后,它传输了错误的信息。也许我错过了一些东西,但我不知道在哪里。
这是我的代码:
@property (nonatomic, retain) NSMutableDictionary *sections;
@property (nonatomic, retain) NSMutableDictionary *sectionToSportTypeMap;
@implementation AllDataViewController
@synthesize sections = _sections;
@synthesize sectionToSportTypeMap = _sectionToSportTypeMap;
- (id)initWithCoder:(NSCoder *)aCoder
{
self = [super initWithCoder:aCoder];
if (self) {
// Custom the table
// The className to query on
self.parseClassName = @"schedule";
self.textKey = @"name";
self.pullToRefreshEnabled = YES;
self.paginationEnabled = YES;
self.objectsPerPage = 25;
self.sections = [NSMutableDictionary dictionary];
self.sectionToSportTypeMap = [NSMutableDictionary dictionary];
}
return self;
}
- (PFQuery *)queryForTable
{
PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];
// If Pull To Refresh is enabled, query against the network by default.
if (self.pullToRefreshEnabled) {
query.cachePolicy = 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 (self.objects.count == 0) {
query.cachePolicy = kPFCachePolicyCacheThenNetwork;
}
[query orderByAscending:@"order"];
return query;
}
- (void) objectsDidLoad:(NSError *)error
{
[super objectsDidLoad:error];
[self.sections removeAllObjects];
[self.sectionToSportTypeMap removeAllObjects];
NSInteger section = 0;
NSInteger rowIndex = 0;
for (PFObject *object in self.objects) {
NSString *sportType = [object objectForKey:@"order"];
NSMutableArray *objectsInSection = [self.sections objectForKey:sportType];
if (!objectsInSection) {
objectsInSection = [NSMutableArray array];
// this is the first time we see this sportType - increment the section index
[self.sectionToSportTypeMap setObject:sportType forKey:[NSNumber numberWithInt:section++]];
}
[objectsInSection addObject:[NSNumber numberWithInt:rowIndex++]];
[self.sections setObject:objectsInSection forKey:sportType];
}
}
- (PFObject *)objectAtIndexPath:(NSIndexPath *)indexPath {
NSString *sportType = [self sportTypeForSection:indexPath.section];
NSArray *rowIndecesInSection = [self.sections objectForKey:sportType];
NSNumber *rowIndex = [rowIndecesInSection objectAtIndex:indexPath.row];
return [self.objects objectAtIndex:[rowIndex intValue]];
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.sections.allKeys.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSString *sportType = [self sportTypeForSection:section];
NSArray *rowIndecesInSection = [self.sections objectForKey:sportType];
return rowIndecesInSection.count;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSString *sportType = [self sportTypeForSection:section];
return sportType;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[super tableView:tableView didSelectRowAtIndexPath:indexPath];
PFObject *selectedObject = [self objectAtIndexPath:indexPath];
}
- (NSString *)sportTypeForSection:(NSInteger)section {
return [self.sectionToSportTypeMap objectForKey:[NSNumber numberWithInt:section]];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"showDataDetail"]) { //showRecipeDetail
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
//PFObject *selectedObject = [self objectAtIndexPath:indexPath];
DataDetailViewController *destViewController = segue.destinationViewController;
PFObject *object = [self.objects objectAtIndex:indexPath.row];
AllData *data = [[AllData alloc] init];
data.title = [object objectForKey:@"title"];
data.imageFile = [object objectForKey:@"imageFile"];
data.date = [object objectForKey:@"date"];
data.information = [object objectForKey:@"information"];
destViewController.data = data;
}
}
答案 0 :(得分:0)
如果您的表格中有多个部分,但无论您选择的部分是什么,您只是偏离第一部分中的信息,可能是因为这一行:
PFObject *object = [self.objects objectAtIndex:indexPath.row];
从self.objects
数组中抓取的对象仅依赖于行而不考虑该部分。
因此,当您的代码现在,如果选择了第2部分中的第1行,假设您的数据是标准顺序(例如A,B,C,D等),您仍然会离开行为了使您的代码按预期工作,您可能需要更改此行:
PFObject *object = [self.objects objectAtIndex:indexPath.row];
到
PFObject *object = [self.objects objectAtIndex:indexPath.row * (indexPath.section + 1)];
这样你就可以乘以(section + 1)来访问self.objects
数组中的正确索引。
答案 1 :(得分:0)
尝试将其嵌套为IF ELSE语句,如此
if (indexPath.section == 0) {
//A
if (indexPath.row == 0) {
PFObject *object = [self.messagesArray objectAtIndex:indexPath.row];
}
}