2012-06-15 55 views
1

我從plist中加載數據的UITableView這樣的:如何設置titleForHeaderInSection手動

- (void)viewDidLoad { 
    [super viewDidLoad]; 


    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); 
    NSString *path = [[documentPaths lastObject] stringByAppendingPathComponent:@"data.plist"]; 

    NSMutableDictionary *resultDic = [[NSMutableDictionary alloc] init]; 
    NSMutableArray *resultArray = [[NSMutableArray alloc] init]; 
    NSDictionary *myDict = [NSDictionary dictionaryWithContentsOfFile:path]; 


    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"purpleKey"]) 
    { 
     NSArray *purple = [myDict objectForKey:@"Purple"]; 
     [resultArray addObject:@"Purple"]; 
     [resultDic setValue:purple forKey:@"Purple"]; 
    } 
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"orangeKey"]) 
    { 
     NSArray *orange = [myDict objectForKey:@"Orange"]; 
     [resultArray addObject:@"Orange"]; 
     [resultDic setValue:orange forKey:@"Orange"]; 
    } 


    self.tableData = resultDic; 
    self.sectionsTitle = resultArray; 

} 

titleForHeaderInSection

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 
    return [sectionsTitle objectAtIndex:section]; 
} 

我的plist結構

enter image description here

我的問題是: 我該怎麼辦爲紫色標題和橙色標題設置標題而不更改plist文件中的名稱? 這樣的:紫=第1類,橙= 2類

編輯

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return sectionsTitle.count; 
} 


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 
    return [sectionsTitle objectAtIndex:section]; 
} 



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    int num = [[tableData objectForKey:[sectionsTitle objectAtIndex:section]] count]; 

    if (num > 3) { 
     num = 3; 
    } 

    return num; 
} 


// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    NSDictionary *dict = [[tableData objectForKey:[sectionsTitle objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row]; 

    cell.textLabel.numberOfLines = 1; 
    cell.textLabel.font = [UIFont systemFontOfSize:11]; 
    cell.textLabel.text = [NSString stringWithFormat:@"%@ - %@", [dict objectForKey:@"Name"], [dict objectForKey:@"Address"]]; 

    if ([dict objectForKey:@"Address"] == (NULL)) { 
     //do nothing 
    } 

    return cell; 
} 
+0

你的意思是應用程序運行時,您可以更改標題,或者你只是想在開發過程中更改爲不同的值? – jrturton

+0

在開發過程中使用不同的值 –

回答

1

從您的代碼,節標頭是從resultsArray來了,你與常量字符串橙色填充的外觀和紫色。

您可以將不同的常量字符串放入該數組中,除非我丟失了某些東西。

所以,與其

[resultArray addObject:@"Orange"]; 

使用

[resultArray addObject:@"Category 1"]; 
+0

是的,它會更改標題,但在該類別爲空之後 –

+0

那麼,您必須在其他位置使用resultArray或self.sectionTitles作爲字典或其他東西的關鍵字。添加一個剛剛用於標題的新數組,並保留鍵的原始數組。 – jrturton

+0

我添加了更多的代碼來提問,你能幫我找到我需要改變的東西嗎? –