1

我按照本教程https://parse.com/questions/using-pfquerytableviewcontroller-for-uitableview-sections

并且能够让我的 PFQueryTableView 控制器与部分一起工作。标题名称基于行的月/年。问题是我的表格末尾没有“加载更多”单元格。

这是我认为需要修改的方法...

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
[super tableView:tableView numberOfRowsInSection:section];
NSString *sportType = [self sportTypeForSection:section];
NSArray *rowIndecesInSection = [self.sections objectForKey:sportType];
if (section == self.sections.count)
    return rowIndecesInSection.count+1;
else
    return rowIndecesInSection.count;
}

PFQueryTableViewController.m

注意:WOD 是 PFObject 的子类

@interface WodLogPFQueryViewController : PFQueryTableViewController
{
    BOOL isSearching;
}
@property (nonatomic, retain) NSMutableDictionary *sections;
@property (nonatomic, retain) NSMutableDictionary *sectionToSportTypeMap;
@end

@implementation WodLogPFQueryViewController
@synthesize sections = _sections;
@synthesize sectionToSportTypeMap = _sectionToSportTypeMap;

- (id)initWithStyle:(UITableViewStyle)style {
    self = [super initWithStyle:style];
    if (self) {

        self.parseClassName = @"WodLog";
        self.pullToRefreshEnabled = YES;
        self.objectsPerPage = 6;
        self.paginationEnabled = YES;
        self.sections = [NSMutableDictionary dictionary];
        self.sectionToSportTypeMap = [NSMutableDictionary dictionary];
    }
    return self;
}

-(PFQuery *)queryForTable {
    PFQuery *query = [WOD query];
    [query whereKey:@"user" equalTo:[PFUser currentUser]];
    [query orderByDescending:@"date"];
    return query;
}

#pragma mark - PFQueryTableViewController

- (NSString *)sportTypeForSection:(NSInteger)section {
    return [self.sectionToSportTypeMap objectForKey:[NSNumber numberWithInt:(int)section]];
}

- (void)objectsDidLoad:(NSError *)error {
    [super objectsDidLoad:error];

    [self.sections removeAllObjects];
    [self.sectionToSportTypeMap removeAllObjects];

    NSInteger section = 0;
    NSInteger rowIndex = 0;
    for (WOD *object in self.objects) {
        NSString *sportType = [object monthYear];
        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:(int)section++]];
        }

        [objectsInSection addObject:[NSNumber numberWithInt:(int)rowIndex++]];
        [self.sections setObject:objectsInSection forKey:sportType];
    }
    [self.tableView reloadData];
}

- (PFObject *)objectAtIndexPath:(NSIndexPath *)indexPath {
    if (self.objects.count != indexPath.row) {
        NSString *sportType = [self sportTypeForSection:indexPath.section];
        NSArray *rowIndecesInSection = [self.sections objectForKey:sportType];
        NSNumber *rowIndex = [rowIndecesInSection objectAtIndex:indexPath.row];
        return [self.objects objectAtIndex:[rowIndex intValue]];
    } else {
        return self.objects[indexPath.row];
    }
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(WOD *)object
{   
    static NSString *identifier = @"WodLogObject";

    WodLogTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    //cell.backgroundColor = [ColorTools colorFromHexString:@"#1f2124"];

    WOD *wodLog = object;

    if (cell == nil) {
        cell = [[WodLogTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        UIButton *delButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [delButton setBackgroundImage:[UIImage imageNamed:@"wodLogListDel"] forState:UIControlStateNormal];
        delButton.frame = CGRectMake(288, 29, 24, 24);
        cell.editingAccessoryView = delButton;
        [delButton addTarget:self action:@selector(cellDeleteClicked:forEvent:) forControlEvents:UIControlEventTouchUpInside];
    }

    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSArray *weekDays = [NSArray arrayWithObjects:@"SUN", @"MON", @"TUE", @"WED", @"THU", @"FRI", @"SAT", nil];

    NSDateComponents *component = [calendar components:NSWeekdayCalendarUnit|NSDayCalendarUnit fromDate:wodLog.date];

    cell.weekDayLabel.text = [weekDays objectAtIndex:([component weekday] - 1)];
    cell.dateLabel.text = [NSString stringWithFormat:@"%ld", (long)[component day]];
    cell.nameLabel.text = wodLog.name;
    cell.descriptionLabel.text = wodLog.desc;
    [cell setResult:wodLog.result forType:wodLog.type];
    cell.prescribedImageView.hidden = ![wodLog.prescribed boolValue];
    cell.perRecImageView.hidden = ![wodLog.personalrecord boolValue];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [super tableView:tableView didSelectRowAtIndexPath:indexPath];

    if ((indexPath.row + 1) > [self.objects count]) {
        NSLog(@"Load More... was tapped");
        [self loadNextPage];
        return;
        //rest of didSelectRowAtIndexPath doesn't run because of return;
    }
    PFObject *selectedObject = [self objectAtIndexPath:indexPath];
    NSLog(@"name = %@", selectedObject[@"name"]);
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.sections.allKeys.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    [super tableView:tableView numberOfRowsInSection:section];
    NSString *sportType = [self sportTypeForSection:section];
    NSArray *rowIndecesInSection = [self.sections objectForKey:sportType];
    if (section == self.sections.count)
        return rowIndecesInSection.count+1;
    else
        return rowIndecesInSection.count;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    NSString *sportType = [self sportTypeForSection:section];
    return sportType;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForNextPageAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"NextPage";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.backgroundColor = [ColorTools colorFromHexString:@"#101113"];

    [cell.textLabel setFont:[UIFont fontWithName:@"HelveticaNeue-Medium" size:18.0f]];
    [[UILabel appearanceWhenContainedIn:[UITableViewCell class], nil]
     setTextColor:[UIColor lightGrayColor]];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.text = @"Load more...";

    return cell;
}

@end
4

0 回答 0