【问题标题】:tableview doesn't see the tableview view for header in section methodtableview 在节方法中看不到标题的 tableview 视图
【发布时间】:2013-09-02 14:54:12
【问题描述】:

我试图为我的tableview.Tableview 创建一个自定义部分本身工作正常,但由于某种原因,编译器跳过,

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

委托方法,我尝试了reference this 上写的内容。我还添加了tableView:heightForHeaderInSection: 方法,正如它在参考页面中所建议的那样,但由于某种原因它只是不读取这些方法。显然,我做错了什么。如果有人能指出我的错误,我将不胜感激。提前谢谢你。

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

#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

【问题讨论】:

    标签: ios objective-c uitableview


    【解决方案1】:

    viewDidLoad 方法中有错字:

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

    其中一个应该是delegate

    【讨论】:

      猜你喜欢
      • 2021-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多