У меня есть plist, хранящийся в Document Directory
, как вызвать UITableView
, если он хранится в каталоге документов
. Вот как я прочитал из plist.
In ViewDidLoad
- (void)viewDidLoad {
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [documentPaths objectAtIndex:0];
NSString *documentPlistPath = [documentsDirectory stringByAppendingPathComponent:@"p.plist"];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:documentPlistPath];
NSArray *valueArray = [dict objectForKey:@"title"];
self.mySections = valueArray;
[super viewDidLoad];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//Return the number of sections.
return [self.mySections count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
NSString *key = [self.mySections objectAtIndex:section];
NSArray *dataInSection = [self.myData objectForKey:key];
return [dataInSection count];
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSString *key = [self.mySections objectAtIndex:section];
return [NSString stringWithFormat:@"%@", key];
}
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return self.mySections;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] ;
}
// Configure the cell...
NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];
NSString *key = [self.mySections objectAtIndex:section];
NSDictionary *dataForSection = [[self.myData objectForKey:key] objectAtIndex:0];
NSArray *array=dataForSection.allKeys;
cell.textLabel.text = [[dataForSection allKeys] objectAtIndex:row];
cell.detailTextLabel.text=[dataForSection valueForKey:[array objectAtIndex:indexPath.row]];
return cell;
}
ИЗМЕНИТЬ, когда я NSlog
valueArray
выводит ( { pass = tryrt; title = tyr; },
{ pass = tryrt; title = tyr; },
{ pass = tryrt; title = tyr; }, { pass = tryrt546546; title = tyr; } )
значит проблема в
cellForRowAtIndexPath
self.myData
является словарем массивов (разделов) словарей (строк) вnumberOfRowsInSection
, но является словарем массивов словарей массивовNSString
вcellForRowAtIndexPath
, это очень запутанно. Я думаю, будет проще, если вы сначала определитесь со структурой источника данных. Если вы хотите прочитать данный .plist вself.myData
и использовать его, неясно, как эти данные представляют разделы и строки. Может быть, вы могли бы объяснить, для чего нужны значенияpass
иtitle
. Кроме того, можете ли вы изменить структуру .plist, если это необходимо? - person A-Live   schedule 12.12.2012pass
иtitle
. Теперь хочу заполнить из plist, и я могу изменить, но затем нужно внести изменения, чтобы сохранить данные в plist - person Christien   schedule 12.12.2012pass
, имеющие одну и ту же паруtitle
, сгруппированы в разделы, чтобы значениеtitle
использовалось в качестве заголовка раздела, а значенияpass
были текстом строк. Вы хотите увидеть что-то подобное? - person A-Live   schedule 12.12.2012