简体   繁体   English

tableview在section方法中看不到标题的tableview视图

[英]tableview doesn't see the tableview view for header in section method

I was trying to create a custom section for my tableview . 我试图为tableview创建一个自定义部分。 Tableview itself works fine, but for some reason, compiler skips, Tableview本身可以正常运行,但是由于某些原因,编译器会跳过,

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

delegate method, and i tried what is written on the reference , and this . 委托方法,我尝试了在referencethis上写的内容。 I also added the tableView:heightForHeaderInSection: method as it suggests in reference page but for some reason it just doesn't read these methods. 我还按照参考页中的建议添加了tableView:heightForHeaderInSection:方法,但由于某种原因,它只是不读取这些方法。 Obviously, I am doing something wrong. 显然,我做错了。 If someone can point my mistake out, I would appreciate. 如果有人可以指出我的错误,我将不胜感激。 Thank you in advance. 先感谢您。

profileViewController.h profileViewController.h

#import <UIKit/UIKit.h>
#import "PeopleModel.h"
#import <QuartzCore/QuartzCore.h>

@interface profileViewController : UIViewController
<UITableViewDataSource,UITableViewDelegate>

@property(strong,nonatomic)NSMutableArray *People;

@property (weak, nonatomic) IBOutlet UITableView *profileTableView;

@end

profileViewController.m profileViewController.m

#import "profileViewController.h"
@interface profileViewController ()
+ (UIImage *)scale:(UIImage *)image toSize:(CGSize)size;
@end

@implementation profileViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"ProfileView";
self.profileTableView.dataSource = self;
    self.profileTableView.dataSource = self;

    PeopleModel *person1 = [[PeopleModel alloc]init];
 person1.Name =@"mett";
 person1.Email= @"mett@gmail.com";
    person1.ProfilePicture =[UIImage imageNamed:@"profileImage.jpg"];
    self.People = [NSMutableArray arrayWithObjects:person1,person1, nil];
    self.profileTableView.layer.cornerRadius = 8.0;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.People count];

}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.textLabel.font = [UIFont systemFontOfSize:19.0];
        cell.detailTextLabel.font = [UIFont systemFontOfSize:12];
    }
    PeopleModel *item =[self.People objectAtIndex:indexPath.row];
    cell.textLabel.text = item.Name;
    cell.detailTextLabel.text = item.Email;
    cell.imageView.image = item.ProfilePicture;
    cell.imageView.image =[profileViewController scale:item.ProfilePicture toSize:CGSizeMake(115, 75)];

return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 15;
}

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    NSLog(@"cartman!");
    if (section == 0) {
        CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
        UIView* headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenRect.size.width, 44.0)];
        //headerView.contentMode = UIViewContentModeScaleToFill;

        // Add the label
        UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10.0, -5.0, 300.0, 90.0)];
        headerLabel.backgroundColor = [UIColor clearColor];
        headerLabel.opaque = NO;
        headerLabel.text = @"section one";
        headerLabel.textColor = [UIColor blueColor];
        headerLabel.highlightedTextColor = [UIColor blackColor];

        //this is what you asked
        headerLabel.font = [UIFont boldSystemFontOfSize:17];

        headerLabel.shadowColor = [UIColor clearColor];
        headerLabel.shadowOffset = CGSizeMake(0.0, 1.0);
        headerLabel.numberOfLines = 0;
        headerLabel.textAlignment = NSTextAlignmentCenter;
        [headerView addSubview: headerLabel];


        // Return the headerView
        return headerView;
    }
    else return nil;
}

+ (UIImage *)scale:(UIImage *)image toSize:(CGSize)size
{
    UIGraphicsBeginImageContext(size);
    [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
    UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return scaledImage;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;

}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

You have a typo in the viewDidLoad method: 您在viewDidLoad方法中有一个错字:

self.profileTableView.dataSource = self;
self.profileTableView.dataSource = self;

One of these should be delegate . 其中之一应该是delegate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM