表视图单元格 - UITableView - 无法在详细视图中显示多行
Posted
技术标签:
【中文标题】表视图单元格 - UITableView - 无法在详细视图中显示多行【英文标题】:Table View Cell - UITableView - Cannot show multiple rows in detailed view 【发布时间】:2015-09-05 09:21:59 【问题描述】:我正在使用 Tableview 来显示来自 recipes Array
的 Name,效果很好,并在来自 recipes Array
的表格视图中显示所有名称。当我点击其中一个食谱时,它会在详细视图中显示名称。
我的困惑是,如何在 detailed view
中显示其他字段。我有一个数组 (array name: ingredients
),其中包含特定配方的内容,例如成分。
我试过了
cell.textLabel.text = [ingredients objectAtIndex:indexPath.row];
RecipeDetailViewController.h
#import <UIKit/UIKit.h>
@interface RecipeDetailViewController : UIViewController
@property (nonatomic, strong) IBOutlet UILabel *recipeLabel;
@property (nonatomic, strong) NSString *recipeName;
@property (strong, nonatomic) IBOutlet UILabel *recipeCountry;
@property (nonatomic, strong) NSString *recipeCountryName;
@end
但它在 RecipeBookViewController.m 中没有显示任何内容 这是我的原始代码
#import "RecipeBookViewController.h"
#import "RecipeDetailViewController.h"
@interface RecipeBookViewController ()
@end
@implementation RecipeBookViewController
NSArray *recipes;
@synthesize tableView;
- (void)viewDidLoad
[super viewDidLoad];
NSData *allCourseData=[[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://api.kivaws.org/v1/loans/search.json?status=fundraising"]];
NSError *error;
NSMutableDictionary *JsonObject = [NSJSONSerialization
JSONObjectWithData:allCourseData options:NSJSONReadingMutableContainers
error:&error];
NSArray *loans = JsonObject[@"loans"];
// Create a new loans array for store the new items
NSMutableArray *newLoans = [NSMutableArray array];
NSMutableArray *arrCountry = [NSMutableArray array];
for (NSDictionary *loan in loans)
NSNumber *ident = loan[@"id"];
NSString *name = loan[@"name"];
NSDictionary *description = loan[@"description"];
NSDictionary *location = loan[@"location"];
NSString *country = location[@"country"];
NSString *status = loan[@"status"];
NSDictionary *geo = location[@"geo"];
NSString *geoprint = geo[@"pairs"];
[newLoans addObject:name];
[arrCountry addObject:country];
recipes=newLoans;
- (void)viewDidUnload
[super viewDidUnload];
// Release any retained subviews of the main view.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
return [recipes count];
//Display Name in Detail View
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *simpleTableIdentifier = @"RecipeCell";
//Displays name
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
cell.textLabel.text = [recipes objectAtIndex:indexPath.row];
return cell;
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
if ([segue.identifier isEqualToString:@"showRecipeDetail"])
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
RecipeDetailViewController *destViewController = segue.destinationViewController;
destViewController.recipeName = [recipes objectAtIndex:indexPath.row];
@end
RecipeDetailViewController.m
#import "RecipeDetailViewController.h"
@interface RecipeDetailViewController ()
@end
@implementation RecipeDetailViewController
@synthesize recipeLabel;
@synthesize recipeName;
@synthesize recipeCountry;
@synthesize recipeCountryName;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
// Custom initialization
return self;
- (void)viewDidLoad
[super viewDidLoad];
// Set the Label text with the selected recipe
recipeLabel.text = recipeName;
recipeCountry.text=recipeCountryName;
- (void)viewDidUnload
[super viewDidUnload];
// Release any retained subviews of the main view.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
return (interfaceOrientation == UIInterfaceOrientationPortrait);
@end
这是两个视图的屏幕截图
【问题讨论】:
您需要向我们展示显示详细视图的代码。 @deadbeef 我添加了 RecipeDetailViewController.m 的代码 显示您导航到 RecipeDetailVIewController 的位置,并显示您的主/成分数组数据。 显示你的 'didSelectRowAtIndexPath' 委托方法。 @Shoaib 代码已更新 【参考方案1】:循环中的变化;
for (NSDictionary *loan in loans)
NSNumber *ident = loan[@"id"];
NSString *name = loan[@"name"];
NSDictionary *description = loan[@"description"];
NSDictionary *location = loan[@"location"];
NSString *country = location[@"country"];
NSString *status = loan[@"status"];
NSDictionary *geo = location[@"geo"];
NSString *geoprint = geo[@"pairs"];
[newLoans addObject:@[
@"name":name,
@"description":description,
@"country ":country
]]; // Same way add all keys
//Or
/*
NSMutableDictionary* dicData = [NSMutableDictionary dictionary];
newLoans[@"name"] = name;
newLoans[@"description"] = description;
newLoans[@"country"] = country;
[newLoans addObject:dicData];
*/
recipes=newLoans;
检索时改变;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
. . .
NSDictionary * dict = [recipes objectAtIndex:indexPath.row];
cell.textLabel.text = dict[@"name"];
return cell;
以同样的方式将完整的字典传递给 RecipeDetailViewController 并在那里显示其他信息。
【讨论】:
这个想法是在你的数组中有一个字典而不是单个字符串。我没有机会在 xcode 中测试代码,因此您必须自己更正语法错误(如果有)。 BTW 更新了帖子。 我的代码已经在使用 cell.textLabel.text = [recipes objectAtIndex:indexPath.row];我的问题是如何在其中添加一个带有国家名称的数组作为 cell.textLabel.text = [countryname objectAtIndex:indexPath.row];以上是关于表视图单元格 - UITableView - 无法在详细视图中显示多行的主要内容,如果未能解决你的问题,请参考以下文章
UITableview 单元格高度在 iOS 14.0 中的滚动表视图上发生变化