ios - tableview 在 section 方法中看不到标题的 tableview View

标签 ios objective-c uitableview

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

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

委托(delegate)方法,我尝试了写在 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

最佳答案

您在 viewDidLoad 方法中有错别字:

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

其中一个应该是 delegate

关于ios - tableview 在 section 方法中看不到标题的 tableview View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18567086/

相关文章:

objective-c - 想要使用 for 循环来设置多个对象的属性

swift - UIButton 无法在 UITableViewCell 中的所有设备上居中 - Swift

ios - Swift ios 这是一个很好的解决方案而不是表格单元格

ios - 如何将 NSDate 转换为相对格式 "Today","Yesterday","a week ago","a month ago","a year ago"?

ios - CGContextSetStrokeColorWithColor UIColor setStroke 有什么区别?

objective-c - IB中的子类化

ios - 通过枚举值循环注册 UITableView 单元格

iphone - Apple iPhone 应用程序转移或应用程序替换

ios - 未为自定义 UITableViewCell 调用 GetHeightForRow

iphone - +实体名称 : could not locate an NSManagedObjectModel for entity name