【问题标题】:PFQueryTableViewController Multiple SectionsPFQueryTableViewController 多个部分
【发布时间】:2014-09-29 01:38:51
【问题描述】:

我正在关注这个论坛:https://parse.com/questions/using-pfquerytableviewcontroller-for-uitableview-sections

如果您转到上面的链接并阅读标题,您会看到我正在尝试在我的PFQueryTableViewController 中创建部分。从上面的链接中复制代码后,我可以很好地分割我的 TableView ......它工作得很好!这是我的问题。 (最好的方法是举一个例子)。想象一下,我有 3 个单元格,每个单元格有 2 个单元格

一个 乙

C D

E F

当我点击 A 时,我得到 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;

    }
}

【问题讨论】:

  • 补充:我很确定这与prepareForSegue 有关,因为当我登录didSelectRowAtIndexPath 时,我得到了正确的信息。
  • 当然,我没有通读您的所有代码,但如果它发生在 prepareForSegue 并且您有多个部分但您只关注第一部分中的信息,也许它是因为这一行:PFObject *object = [self.objects objectAtIndex:indexPath.row];objects 数组中抓取的对象仅依赖于行而不考虑节。因此,如果选择了第 2 节中的第 1 行,假设您的数据是标准顺序,您仍然会转到第 1 节中的第 1 行。
  • 我要写的内容超出了评论的范围,所以我会提供一个正确的答案...如果我走错了路,请随时告诉我。

标签: ios objective-c uitableview parse-platform


【解决方案1】:

如果您的表格中有多个部分,但无论您选择了哪个部分,您都只关注第一部分中的信息,也许是因为这一行:

PFObject *object = [self.objects objectAtIndex:indexPath.row];

self.objects 数组中抓取的对象仅取决于行而不考虑节。

因此,就您现在的代码而言,如果选择了第 2 部分中的第 1 行,假设您的数据是标准顺序(例如 A、B、C、D 等),您仍将继续选择第 1 行第 1 节。要使您的代码按预期工作,您可能必须更改此行:

PFObject *object = [self.objects objectAtIndex:indexPath.row];

PFObject *object = [self.objects objectAtIndex:indexPath.row * (indexPath.section + 1)];

这样您就可以乘以 (section + 1) 以访问 self.objects 数组中的正确索引。

【讨论】:

  • 非常感谢!出于某种原因,尽管这仍然不起作用,哈哈!不过,您肯定会正确地思考某些事情。
【解决方案2】:

尝试像这样嵌套一个 IF ELSE 语句

if (indexPath.section == 0) {
        //A
        if (indexPath.row == 0) {
            PFObject *object = [self.messagesArray objectAtIndex:indexPath.row];
        }
    }

【讨论】:

  • 我不知道最后会有多少行和多少节
猜你喜欢
  • 2014-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多