2014-11-05 73 views
-1

我试图创建一个自定义viewForHeaderInSectiontableview.Tableview本身 工作正常,但由于某些原因,编译器跳过,的tableview没有看到标题为标题在第方法

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{}  

代表法,我尝试了参考文献上的内容,但是由于某些原因,它只是不会读取这些方法。显然,我做错了什么。如果有人能指出我的错误,我将不胜感激。先谢谢你。

#import "TableViewController.h" 

@interface TableViewController() 

@end 

@implementation TableViewController 
@synthesize settingTableView,audioPlayer,labelSwitch,descriptiveSwitch,autoPlaySwitch;; 

- (id)initWithStyle:(UITableViewStyle)style 
{ 
    self = [super initWithStyle:style]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.settingTableView.tableFooterView = [[UIView alloc]init]; 
    self.settingTableView.scrollEnabled = NO; 

    settings = @{@"Animation Sets" : @[@"Set1",@"Set2",@"Set3"],@"Settings" : @[@"Label",@"Sound",@"Autoplay"]}; 
    values = [[settings allKeys]sortedArrayUsingSelector:@selector(localizedStandardCompare:)]; 

    [settingTableView setRowHeight:44]; 
    [settingTableView reloadData]; 

} 

-(void)close:(id)sender 
{ 
    [self dismissViewControllerAnimated:YES completion:nil]; 
    [self.delegate closeView]; 
} 

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

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    return [values count]; 
} 
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 

    return [values objectAtIndex:section]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // Return the number of rows in the section. 
    NSString *sectionTitle = [values objectAtIndex:section]; 
    NSArray *sectionValue = [settings objectForKey:sectionTitle]; 
    return [sectionValue count]; 
} 

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 
    CGRect frame = tableView.frame; 
    NSString *value = [values objectAtIndex:section]; 

    UILabel *title = [[UILabel alloc]init]; 
    UIView *headerView = [[UIView alloc]init]; 

    //if (section == 0 && section == 1) { 

    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) { 
     title.frame = CGRectMake(10,10,150,30); 
    } 
    else 
    { 
     title.frame = CGRectMake(10,5,100,30); 
    } 

    title.backgroundColor = [UIColor clearColor]; 
    title.text = value; 

    headerView.frame = CGRectMake(0,0,frame.size.width,frame.size.height); 
    headerView.backgroundColor = [UIColor clearColor]; 
    [headerView addSubview:title]; 
     [headerView setBackgroundColor:[UIColor blueColor]]; 
    //} 
    return headerView; 
    [settingTableView reloadData]; 
    } 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell* cell = nil; 

    static NSString *MyCellIdentifier = @"Cell"; 

    cell = [tableView dequeueReusableCellWithIdentifier:nil]; 

    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
             reuseIdentifier:MyCellIdentifier]; 

     cell.textLabel.numberOfLines = 2; 
     cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:17.0]; 

     NSString *sectionTitle = [values objectAtIndex:indexPath.section]; 
     NSArray *sectionValue= [settings objectForKey:sectionTitle]; 
     NSString *settingValues = [sectionValue objectAtIndex:indexPath.row]; 

     cell.textLabel.text = settingValues; 
     return cell; 
    } 
} 
+0

添加代码找到你错在哪里 – 2014-11-05 07:05:39

+0

我已经添加了上面的代码。请检查 – Nanda 2014-11-05 07:31:10

+0

删除此行'[settingTableView reloadData];'在查看中打印 – 2014-11-05 09:01:38

回答

0

你需要确保实现该委托方法的类是delegate和你的表视图dataSource

+0

是的,我已经在.h文件中实现委托和数据源方法,并且也面临上述问题。 – Nanda 2014-11-05 07:13:36

+0

单元格的内容和一切工作正常? 'titleForHeaderInSection'里面有什么? – 2014-11-05 07:14:44

+0

单元格为行工作罚款只有在编译时不调用titleForHeaderInSection方法。 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { return [values objectAtIndex:section]; } – Nanda 2014-11-05 07:16:22