【问题标题】:Populating Plist to UITableView,Stored in Document Directory将Plist填充到UITableView,存储在文档目录中
【发布时间】: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.myDatanumberOfRowsInSection 处的字典(行)的数组(部分)的字典,但它是数组字典的数组字典NSStringcellForRowAtIndexPath,这非常令人困惑。我认为如果您先确定数据源结构会更容易。如果您想将给定的 .plist 读入 self.myData 并使用它,则不清楚这些数据如何表示部分和行。也许您可以解释一下passtitle 的值是什么。另外,如果需要,您可以更改 .plist 结构吗?
  • 就像我有两个文本框来存储这些值,我存储到 passtitle。现在想从 plist 填充,我可以更改,但需要进行更改以将数据存储到 plist
  • 好吧,我可以想象将具有相同配对 titlepass 值分组为部分,以便将 title 值用作部分标题,pass 值是行文本。你想看这样的东西吗?

标签: iphone ios uitableview plist nsdocumentdirectory


【解决方案1】:
- (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.
NSArray *allKeys = [[self.mySections objectAtIndex:section] allKeys];
return [allKeys count];
}


- (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];
NSArray *allKeys = [[self.mySections objectAtIndex:section] allKeys];
cell.textLabel.text = [[self.mySections objectAtIndex:section] objectForKey:[allKeys objectAtIndex:0]];    
cell.detailTextLabel.text=[[self.mySections objectAtIndex:section] objectForKey:[allKeys objectAtIndex:1]];

return cell;
}

【讨论】:

  • 谢谢!它现在工作正常。但问题是......在表格视图中......每个值显示两次......意味着每个人的第 1 行和第 2 行中的重复值
  • 如果我给 return 1;在numberofrowsinsection...然后输出很好
  • 现在享受...那个问题是因为我不知道你到底想要什么..:)]
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-30
  • 2013-05-09
  • 2016-09-19
  • 1970-01-01
相关资源
最近更新 更多