【发布时间】:2012-12-18 05:35:38
【问题描述】:
我有一个带有字典的 plist 和每个字典的字符串数。显示到下面的 url 中。这个项目列表在 plist 中。它给了我输出但是......没有正确显示..我认为问题在CellForRowatindexPath 中,如果 NSLOG valueArray 输出正确
我需要将这些 plist 数据显示到 UItableview 中
.
如何做到这一点?
我的代码:
- (void)viewWillAppear:(BOOL)animated
{
// get paths from root direcory
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [documentPaths objectAtIndex:0];
NSString *documentPlistPath = [documentsDirectory stringByAppendingPathComponent:@"p.plist"];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:documentPlistPath];
valueArray = [dict objectForKey:@"title"];
self.mySections=[valueArray copy];
NSLog(@"value array %@",self.mySections);
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [self.mySections count];
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSString *key = [[self.mySections objectAtIndex:section]objectForKey:@"pass"];
return [NSString stringWithFormat:@"%@", key];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.mySections 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];
cell.textLabel.text = [[self.mySections objectAtIndex:row] objectForKey:@"title"];
cell.detailTextLabel.text=[[self.mySections objectAtIndex:section] objectForKey:[allKeys objectAtIndex:1]];
return cell;
}
【问题讨论】:
-
如果您认为问题出在
cellForRowAtIndexPath,请设置断点并单步执行代码并查看错误值出现的位置。或者至少使用 NSLog 并找出答案。 -
查看 mySections 数组的打印输出而不是 plist 会更有用。还有,怎么了?您没有描述问题——“没有正确显示”信息量不是很大。
-
你是否正确设置了 UITableViewDataSource
-
另外,你真的要返回 self.mySections.count 的节数和行数吗?
-
@rdelmar if i nslog { value array ( { pass = item; title = item; }, { Protein = ""; carbs = ""; fats = ""; pass = ""; title = ""; }, { 蛋白质 = 7; 碳水化合物 = 7; 脂肪 = 7; pass = 糖; 标题 = 10; }, { pass = hi; title = hii; })
标签: iphone ios uitableview ios5 plist