显示从plist到tableCell的数据

时间:2015-01-20 12:35:23

标签: ios xcode uitableview plist

我有一个龙头,其屏幕截图如下:

plist

我想在表格单元格中显示其数据。我的代码是

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section
{
    return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
NSString *path = [[NSBundle mainBundle] pathForResource:@"datalist" ofType:@"plist"];

// Load the file content and read the data into arrays
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
tableData = [dict objectForKey:@"title"];
    thumbnails = [dict objectForKey:@"Subtitle"];
cell.textLabel.text = [NSString stringWithFormat:@"%@",thumbnails];
return cell;
}

我收到了这个输出:

Output screen

2 个答案:

答案 0 :(得分:1)

试试这个:

NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"datalist.plist"];

if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath])
{
    plistPath = [[NSBundle mainBundle] pathForResource:@"datalist" ofType:@"plist"];
}

NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
tableData = [dict objectForKey:@"title"];
thumbnails = [dict objectForKey:@"Subtitle"];

cell.textLabel.text = [NSString stringWithFormat:@"%@",[thumbnails objectAtIndex:indexPath.row]];
return cell;

缩略图是一个数组。因此,当您从Array中检索数据时,请使用[thumbnails objectAtIndex:indexPath.row]

答案 1 :(得分:1)

您的代码thumbnails = [dict objectForKey:@"Subtitle"];将返回NSArray。 您应该做的是遍历此数组并获取每个项目。

cell.textLabel.text = [NSString stringWithFormat:@"%@",[thumbnails objectAtIndex:0]]cell.textLabel.text = [NSString stringWithFormat:@"%@",[thumbnails objectAtIndex:1]]

或许可能是这样的

cell.textLabel.text = [NSString stringWithFormat:@"%@ %@",[thumbnails objectAtIndex:0], [thumbnails objectAtIndex:0]]

试试吧。