У меня есть представление предпочтений, которое показывает другое представление таблицы в зависимости от того, какой сегментный элемент управления нажат.
Я жестко запрограммировал некоторые NSMutableArrays для проверки основных принципов:
prefsIssuesList = [[NSMutableArray alloc] init];
[prefsIssuesList addObject:@"Governance"];
[prefsIssuesList addObject:@"Innovation and technology"];
...etc
prefsIndustriesList = [[NSMutableArray alloc] init];
[prefsIndustriesList addObject:@"Aerospace and defence"];
... etc
prefsServicesList = [[NSMutableArray alloc] init];
[prefsServicesList addObject:@"Audit and assurance"];
...etc
currentArray = [[NSMutableArray alloc] init];
currentArray = self.prefsIssuesList;
Затем перезагрузите представление таблицы с помощью currentArray, добавив UITableViewCellAccessoryCheckmark.
Все отлично работает.
Но теперь я хочу сохранить или выключить флажок в файле pList и прочитать его обратно.
В идеале хочу листать вот так
Root Dictionary
Issues Dictionary
Governance Number 1
Innovation and technology Number 0
etc
Я дошел до того, что решил это
// Designate plist file
NSString *path = [[NSBundle mainBundle] pathForResource: @"issues" ofType:@"plist"];
// Load the file into a Dictionary
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
self.allNames= dict;
[dict release];
NSLog(@"Dict is %@", allNames); // All the data in the pList file
NSMutableArray *issueSection = [allNames objectForKey:@"Issues"];
NSLog(@"Issues is %@", issueSection); // The data is the Issues Section
NSString *issueVal = [issueSection objectForKey:@"Governance"];
NSLog(@"Governance is %@", issueVal); //The value of the Governance key
Но что я действительно хочу сделать, так это перебрать словарь проблем и получить пары ключ / значение, чтобы
key = cell.textLabel.text
value = UITableViewCellAccessoryCheckmark / UITableViewCellAccessoryNone
depending wether it's 1 or 0
Я предполагаю, что я все еще могу назначить один из трех NSMutableArrays для currentArray, как я это делал в жестко запрограммированной версии, и использовать currentArray для перезагрузки просмотра таблицы.
Затем исправьте этот код, чтобы построить табличное представление
NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];
NSString *key = [keys objectAtIndex:section];
NSArray *nameSection = [names objectForKey:key];
static NSString *CellIdentifier = @"Cell";
//UITableViewCell *cell = [self.prefsTableView dequeueReusableCellWithIdentifier:SectionsTableIdentifier];
UITableViewCell *cell = [self.prefsTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil) {
cell=[[[UITableViewCell alloc]
initWithFrame:CGRectZero
reuseIdentifier: CellIdentifier] autorelease];
}
cell.textLabel.text = [nameSection objectAtIndex:row];
return cell;
Но мой мозг растаял, сегодня я потратил около шести часов на чтение списков pLists, NSArrays, NSMutableDisctionaries, standardUserDefa, по-видимому, мало что дает.
Мне удалось использовать UITableViews внутри UINavigationViews, использовать SegmentedControls, загрузить асинхронный XML, но теперь я, наконец, застрял, или сгорел, или и то, и другое. Над тем, что должно быть довольно простыми парами ключ / значение.
Кто-нибудь хочет дать мне несколько идиотских указателей?