显示从 plist 到 tableCell 的数据

display data from plist to tableCell

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

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

-(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;
}

我得到这个输出:

试试这个:

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;

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

您的代码 thumbnails = [dict objectForKey:@"Subtitle"]; 将 return 一个 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]]

试试吧。