2010-04-29 79 views
0

即时通讯设置我的部分标题在uitableview中的问题,它可能是一个非常简单的东西,我只是不能解决它。在UITableView部分标题的问题

,而不是为不同部分显示不同的页眉它显示了每个部分

帮助相同的标题我请:)

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    WorldCupAppDelegate *appDelegate = [UIApplication sharedApplication].delegate; 

    return [appDelegate.matchFixtures count]; 
} 

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


    WorldCupAppDelegate *appDelegate = [UIApplication sharedApplication].delegate; 
    Fixtures *fixtures = [appDelegate.matchFixtures objectAtIndex:section]; 

    return fixtures.matchDate; 

} 
+0

您是否检查过appDelegate.matchFixtures count是否有通过NSLog打印的值?相同的fixtures.matchDate? – 2010-04-29 13:16:22

+0

每个matchFixture都有不同的matchDate吗? – DyingCactus 2010-04-29 13:24:42

回答

1

尝试这样

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:  (NSInteger)section { 
NSString *title = nil; 
// Return a title or nil as appropriate for the section. 
switch (section) { 
    case 0: 
     title = [[appDelegate.matchFixtures objectAtIndex:section]matchDate]; 
     break; 
    case 1: 
     title = [[appDelegate.matchFixtures objectAtIndex:section]matchDate]; 
     break; 
    case 2: 
     title = [[appDelegate.matchFixtures objectAtIndex:section]matchDate]; 
     break; 
    default: 
     break; 
} 
return title; 
} 

编辑

使用委托类中的保留正确设置日期在代理中存储值时,我遇到了一些类似的问题。

- (void)setCurrentDates:(NSString *)value { 
[value retain]; // <- Retain new value 
[date release]; // <- Release old value; 
date = value; 
} 

一切顺利。

+0

看起来是正确的,但它崩溃我的应用程序 与 2010-04-29 14:25:51.729 WorldCup [4912:207] *** - [灯具长度]:无法识别的选择器发送到实例0x3b0fe30 2010-04 -29 14:25:51.730 WorldCup [4912:207] ***由于未捕获的异常'NSInvalidArgumentException',原因:'*** - [灯具长度]:无法识别的选择器发送到实例0x3b0fe30' 2010-04- 29 14:25:51.731 WorldCup [4912:207] Stack:( 任何想法为什么? – 2010-04-29 13:27:17

+0

打印并检查从代表获取的任何一个日期值 – Warrior 2010-04-29 14:43:20

+0

根据您问题中的代码,此答案中的代码应为大写0: title = [[appDelegate.matchFixtures objectAtIndex:section] matchDate]; – 2010-04-29 15:04:57

3

您的原始代码看起来不错。我敢打赌,appDelegate.matchFixtures不包含你认为它的数据。修改您的代码如下所示:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    WorldCupAppDelegate *appDelegate = [UIApplication sharedApplication].delegate; 

    NSLog(@"appDelegate.matchFixtures.count = %i", appDelegate.matchFixtures.count); 

    return [appDelegate.matchFixtures count]; 
} 

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

    WorldCupAppDelegate *appDelegate = [UIApplication sharedApplication].delegate; 
    Fixtures *fixtures = [appDelegate.matchFixtures objectAtIndex:section]; 

    NSLog(@"For section %i, fixtures.matchDate = %@", section, fixtures.matchDate); 

    return fixtures.matchDate; 
} 

并查看调试控制台中的日志输出。