2010-04-29 42 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; 
} 

並查看調試控制檯中的日誌輸出。