수동

2012-06-15 2 views
1
내가 이런 식으로 jQuery과에 PLIST에서 데이터를로드

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

당신이 상수 문자열 오렌지와 채우기하는 코드 섹션 헤더 resultsArray에서 오는,의 모습에서 편집

- (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

및 자주색.

내가 뭔가를 놓치지 않으면 다른 배열을 배열에 넣을 수 있습니다.

그래서 대신 사용

[resultArray addObject:@"Orange"]; 

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

예, 헤더가 변경되지만 해당 카테고리가 비어있는 경우 –

+0

글쎄, resultArray 또는 self.sectionTitles를 다른 곳의 키처럼 사용하고 있어야합니다. 제목에 사용 된 새로운 배열을 추가하고 원래의 배열을 키로 유지하십시오. – jrturton

+0

질문에 더 많은 코드를 추가했습니다. 변경해야 할 항목을 찾도록 도와 줄 수 있습니까? –