【发布时间】:2012-11-30 02:28:32
【问题描述】:
我有plist存储在Document Directory,如果它存储在文档目录中,如何调用UITableView
. 这就是我从 plist 中读取的方式。
在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
【问题讨论】:
-
您在 .plist 中有一个字典数组。
-
ya..代码中需要哪些更改..帮助
-
这段代码本身看起来不太好,它假定
self.myData是numberOfRowsInSection处的字典(行)的数组(部分)的字典,但它是数组字典的数组字典NSString的cellForRowAtIndexPath,这非常令人困惑。我认为如果您先确定数据源结构会更容易。如果您想将给定的 .plist 读入self.myData并使用它,则不清楚这些数据如何表示部分和行。也许您可以解释一下pass和title的值是什么。另外,如果需要,您可以更改 .plist 结构吗? -
就像我有两个文本框来存储这些值,我存储到
pass和title。现在想从 plist 填充,我可以更改,但需要进行更改以将数据存储到 plist -
好吧,我可以想象将具有相同配对
title的pass值分组为部分,以便将title值用作部分标题,pass值是行文本。你想看这样的东西吗?
标签: iphone ios uitableview plist nsdocumentdirectory