自定义headerView中没有显示任何内容

时间:2014-10-10 00:17:57

标签: ios uitableview

当我运行应用程序时,标题视图容器空间就在那里,并且是可见的。我试图在标题视图中显示用户名,但没有显示任何内容。到目前为止,这是我在tableView中的代码:

#import "HomeView.h"

@interface HomeView () <UIActionSheetDelegate, UIImagePickerControllerDelegate>

@end

@implementation HomeView

- (void)viewDidLoad {
    [super viewDidLoad];

    if ([PFUser currentUser]) {
        NSLog(@"Welcome to the App, %@", [self.user objectForKey:@"username"]);
    } else {
        LoginView *loginView = [[LoginView alloc] init];
        [loginView setHidesBottomBarWhenPushed:YES];
        [loginView.navigationItem setHidesBackButton:YES];
        [self.navigationController pushViewController:loginView animated:NO];
    }

    UIBarButtonItem *takePhoto = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(takePhoto:)];
    self.navigationItem.rightBarButtonItem = takePhoto;

//    [self.tableView registerClass:[HomeViewCell class] forCellReuseIdentifier:@"cellIdentifier"];
//    [self.tableView registerNib:[UINib nibWithNibName:@"HomeViewCell" bundle:nil]
//         forCellReuseIdentifier:@"cellIdentifier"];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
}

- (id)initWithStyle:(UITableViewStyle)style {
    self = [super initWithStyle:style];
    if (self) {
        self.reusableSectionHeaderViews = [NSMutableSet setWithCapacity:3];

    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    PFQuery *query = [PFQuery queryWithClassName:@"UserPhotos"];
    [query orderByDescending:@"createdAt"];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (error) {
            NSLog(@"Error: %@ %@", error, [error userInfo]);
        } else {
            self.userPhotos = objects;
            NSLog(@"Retrieved objects: %@", self.userPhotos);
            [self.tableView reloadData];
        }
    }];
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    NSInteger sections = self.userPhotos.count;
    // Return the number of sections.
    return sections;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return 1;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == self.userPhotos.count) {
        // Load More section
        return nil;
    }

    HomeHeaderView *headerView = [self dequeueReusableSectionHeaderView];

    if (!headerView) {
        headerView = [[HomeHeaderView alloc] initWithFrame:CGRectMake( 0.0f, 0.0f, self.view.bounds.size.width, 44.0f) buttons:HomePhotoHeaderButtonsDefault];
        headerView.delegate = self;
        [self.reusableSectionHeaderViews addObject:headerView];
    }

    //Setting the username display.
    PFObject *owner = [self.userPhotos objectAtIndex:section];
    PFUser *user = [owner objectForKey:@"user"];
    [user fetchIfNeededInBackgroundWithBlock:^(PFObject *object, NSError *error) {
        if (!error) {
            NSString *username = user.username;
            [headerView.userButton setTitle:username forState:UIControlStateNormal];
        }
    }];

    return headerView;
}

- (HomeHeaderView *)dequeueReusableSectionHeaderView {
    for (HomeHeaderView *sectionHeaderView in self.reusableSectionHeaderViews) {
        if (!sectionHeaderView.superview) {
            // we found a section header that is no longer visible
            return sectionHeaderView;
        }
    }

    return nil;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"cellIdentifier";
    HomeViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(cell == nil)
    {
        cell = [[HomeViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    //Setting the image in the cell.
    PFObject *carPhoto = [self.userPhotos objectAtIndex:indexPath.row];
    PFFile *imageFile = [carPhoto objectForKey:@"imageFile"];
    NSURL *imageFileUrl = [[NSURL alloc] initWithString:imageFile.url];
    NSData *imageData = [NSData dataWithContentsOfURL:imageFileUrl];
    cell.carImage.contentMode = UIViewContentModeScaleAspectFit;
    cell.carImage.image = [UIImage imageWithData:imageData];


    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 269;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == self.userPhotos.count) {
        return 0.0f;
    }
    return 44.0f;
}

1 个答案:

答案 0 :(得分:0)

[user fetchIfNeededInBackgroundWithBlock:^(PFObject *object, NSError *error) {
    if (!error) {
        NSString *username = user.username;
        [headerView.userButton setTitle:username forState:UIControlStateNormal];
    }
}];

上面的代码,您正在从后台线程更新headerview UI。您需要包装UI部分,以便它在主线程上执行。

dispatch_async(dispatch_get_main_queue(), ^{
    [headerView.userButton setTitle:username forState:UIControlStateNormal];
};

但是,您需要小心,您正在执行后台操作,然后更新您正在重用的标头。在操作完成之前,有可能在新部分重用标头,然后使用该部分的错误用户名进行设置。如果在操作完成之前标题滚出屏幕,则可以不重复使用标题或取消网络操作来处理此问题。