2012-06-14 3 views
0

Я попытался загрузить мой Array s, как это, но он загружает только первый Array, а другие не отображаются.Как показать и скрыть массив в uitableview

- (void)viewDidLoad { 
    [super viewDidLoad]; 


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

    NSDictionary *myDict = [NSDictionary dictionaryWithContentsOfFile:path]; 



    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"blackKey"]) 
    { 
     NSArray *black = [myDict objectForKey:@"Black"]; 
     self.tableData = [black copy]; 
    } 
    else if ([[NSUserDefaults standardUserDefaults] boolForKey:@"greenKey"]) 
    { 
     NSArray *green = [myDict objectForKey:@"Green"]; 
     self.tableData = [green copy]; 
    } 
    else if ([[NSUserDefaults standardUserDefaults] boolForKey:@"purpleKey"]) 
    { 
     NSArray *purple = [myDict objectForKey:@"Purple"]; 
     self.tableData = [purple copy]; 
    } 
    else if ([[NSUserDefaults standardUserDefaults] boolForKey:@"orangeKey"]) 
    { 
    NSArray *orange = [myDict objectForKey:@"Orange"]; 
     self.tableData = [orange copy]; 
    } 
    else if ([[NSUserDefaults standardUserDefaults] boolForKey:@"blueKey"]) 
    { 
     NSArray *blue = [myDict objectForKey:@"Blue"]; 
     self.tableData = [blue copy]; 
    } 
    else if ([[NSUserDefaults standardUserDefaults] boolForKey:@"redKey"]) 
    { 
     NSArray *red = [myDict objectForKey:@"Red"]; 
     self.tableData = [red copy]; 
    } 
    else if ([[NSUserDefaults standardUserDefaults] boolForKey:@"darkblueKey"]) 
    { 
     NSArray *darkblue = [myDict objectForKey:@"Darkblue"]; 
     self.tableData = [darkblue copy]; 
    } 
    else if ([[NSUserDefaults standardUserDefaults] boolForKey:@"yellowKey"]) 
    { 
     NSArray *yellow = [myDict objectForKey:@"Yellow"]; 
     self.tableData = [yellow copy]; 
    } 


    } 
  1. Что я делаю неправильно?

  2. Также, я хотел бы показать название массива как заголовок раздела, как это можно сделать ?

  3. И есть ли способ ограничить ряды на раздел (мой массив)?

РЕДАКТИРОВАТЬ

// Customize the number of rows in the table view. 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [tableData count]; 
} 



// 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 objectAtIndex:indexPath.row]; 
    cell.textLabel.text = [NSString stringWithFormat:@"%@ - %@", [dict objectForKey:@"Name"], [dict objectForKey:@"Address"]]; 

    return cell; 
} 

EDIT2

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 
    [sectionsTitle objectForKey:[sectionTitle objectAtIndex:indexPath.section]]; 
} 

Использование необъявленной идентификатора 'indexPath'; Вы имели в виду «NSIndexPath»?

ответ

1

Так как вы собираетесь делить их на части, Ваш tableData должен быть NSMutableDictionary И вы также должны держать массив ключей для словаря Как это

NSMutableDictionary *resultDic = [[NSMutableDictionary alloc] init]; 
NSMutableArray *resultArray = [[NSMutableArray alloc] init]; 
NSDictionary *myDict = [NSDictionary dictionaryWithContentsOfFile:path]; 
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"blackKey"]) 
{ 
    NSArray *black = [myDict objectForKey:@"Black"]; 

    //Add the key here 
    [resultArray addObject:@"Black"]; 
    //Add the key and value 
    [resultDic setValue:black forKey:@"Black"]; 
} 
else if ([[NSUserDefaults standardUserDefaults] boolForKey:@"greenKey"]) 
{ 
    NSArray *green = [myDict objectForKey:@"Green"]; 

    //Add the key here 
    [resultArray addObject:@"Green"]; 
    //Add the key and value 
    [resultDic setValue:black forKey:@"Green"]; 
} 
//Repeat for all the elements 
.... 
//Then after you finished all the tests and additions 

//Assign the result array to your tableData 
self.tableData = resultDic; 
self.sectionsTitle = resultArray; 

В настоящее время в обиход sectionsTitle и tableData заполнить таблицу для любого раздела в таблице, вы можете получить свое название от [sectionsTitle objectAtIndex:sectionIndex]; чтобы получить все элементы в секции используют [sectionsTitle objectForKey:[sectionTitle objectAtIndex:sectionIndex]];

+0

Спасибо , похоже, это работает! Можете ли вы помочь мне с другими вопросами? Как я могу настроить свой массив (черный, зеленый) на заголовок раздела? –

+0

@PavelKaljunen проверить мой обновленный ответ –

+0

когда я установил tableData как NSMutableDictionary У меня возникла проблема с ARC «Нет видимого @interface для« NSMutableDictionary »объявляет селектор« objectAtIndex: »« –

Смежные вопросы