如果声明detailViewController与tableview
所以我想填充一个UIViewController从另一个tableviewController推送tableview。我知道我必须写一个if语句,但是在这方面遇到了问题...重申我猜我的问题是如何填充tableViewController中的一个detailedViewController中的tableView?请参阅下面的代码以供参考,如果我没有提供所需的一切,我可以提前道歉。我可能会完全朝这个方向发展。如果声明detailViewController与tableview
感谢您的任何和所有输入!
/* DetaiLViewController.m file */
#import "DetaiViewController.h"
#import "RecommendationsCell.h"
/* .......... */
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.navigationItem.title = _DetailModal[0];
/*WHAT IS THE IF STATEMENT ???!! */
if ([self.navigationItem.title isEqualToString: @"London, United Kingdom"]) {
DetailedImages.image = /* HERE IS AN ARRAY OF London IMAGES to DISPLAY ON DETAILEDVIEWCONTROLLER */
IF....
DetailedImages.image = /* HERE IS AN ARRAY OF LONDON IMAGES to DISPLAY ON DETAILEDVIEWCONTROLLER */
IF....
DetailedImages.image = /* HERE IS AN ARRAY OF BERLIN IMAGES to DISPLAY ON DETAILEDVIEWCONTROLLER */
IF...
DetailedImages.image = /* HERE IS AN ARRAY OF PERTH IMAGES to DISPLAY ON DETAILEDVIEWCONTROLLER */
IF...
THE REMAINING ARRAYS FOR EACH CELL FROM TABLEVIEWCONTROLLER ETC.......
];
}
/* HERE IS ARRAY FROM TABLEVIEWCONTROLLER THAT PUSHES TO DETAILEDVIEWCONTROLLER */
/* _Images = @[@"london.jpeg",
@"berlin.jpeg",
@"perth.jpeg",
@"beijing.jpeg",
@"Madrid-26512.jpg",
@"barcelona.jpeg",
@"norway1_2141015b.jpg",
@"sweden.jpeg",]; */
/* HERE IS ARRAY FROM TABLEVIEWCONTROLLER THAT PUSHES TO DETAILEDVIEWCONTROLLER */
/* _Title = @[@"London, United Kingdom",
@"Berlin, Germany",
@"Perth, Australia",
@"Beijing, China",
@"Madrid, Spain",
@"Barcelona, Spain",
@"Oslo, Norway",
@"Denmark, Sweden",]; */
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return _Title.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"RecommendationsCell";
RecommendationsCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
int row = [indexPath row];
cell.TitleLabel.text = _Title[row];
//cell.DescriptionLabel.text = _Description[row];
cell.ThumbImage.image = [UIImage imageNamed:_Images[row]];
return cell;
}
/* DetailedViewController.h file */
@interface DetaiViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
IBOutlet UIImageView *DetailedImages;
}
//@property (nonatomic, strong) NSArray *Description;
@property (nonatomic, strong) NSArray *Title;
@property (nonatomic, strong) NSArray *Images;
@property (strong, nonatomic) NSArray *DetailModal;
@end
/* DetailedViewController Cell.h file */
@interface RecommendationsCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UILabel *TitleLabel;
@property (strong, nonatomic) IBOutlet UIImageView *ThumbImage;
//@property (strong, nonatomic) IBOutlet UILabel *DescriptionLabel;
@property (strong, nonatomic) IBOutlet UIImageView *DetailedImages;
@end
我不要将您的导航称号的决定。将标题传递给您的详细视图控件。添加像这样DetailViewController.h
@property(nonatomic,retain)NSString * selectedTitle;
在您的if语句中,检查该值并执行您的条件。
更好的解决方案是创建表示数据的对象并将其传递给您的详细视图控制器。这将消除您的条件并使代码更容易添加其他标题。
我会有一个对象的NSString的标题和NSArray的图像显示该位置。然后在你的父视图控制器上,你将拥有一个拥有这些对象的NSArray,这将是你的表数据源。
如果你想更多地分离它,你可以创建一个类来管理你的数据,例如一个LocationRepository,你可以有一个方法来创建数组并填充你的数据,这样任何视图控制器都可以获取这些数据。
if语句为{}之间的代码提供一些条件。在你的情况下,当你的navigationItem.title将是“伦敦,英国”时,{}中的所有代码都将被处理。你可以给这样的更多条件:
if ([self.navigationItem.title isEqualToString: @"London, United Kingdom"]) {
//do this
DetailedImages.image = /* HERE IS AN ARRAY OF London IMAGES to DISPLAY ON DETAILEDVIEWCONTROLLER */
} else if ([self.navigationItem.title isEqualToString: @"Berlin, Germany"]){
//or do this
DetailedImages.image = /* HERE IS AN ARRAY OF LONDON IMAGES to DISPLAY ON DETAILEDVIEWCONTROLLER */
} else {
.......... // if no condition passed do that
}
希望它可以帮助回答你关于if语句的问题。
给你的填充问题。在你的代码中有一些用于numberOfRowsInSection的_Title.count。我不知道那是什么类型的数组,但:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"RecommendationsCell";
RecommendationsCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
Title * title = [self.title objectAtIndex:indexPath.row];
Images *image = [self.images objectAtIndex:indexPath.row];
cell.TitleLabel.text = title;
//cell.DescriptionLabel.text = _Description[row];
cell.ThumbImage.image = [UIImage imageNamed:image];
return cell;
}
好的,谢谢。创建对象是有道理的。所以只是为了澄清,如果我的tableViewController有8个单元格,我会创建8个NSObject类(标题)...然后使用父视图控制器的实现文件中的NSArray图像集创建每个对象的实例?不要挑剔,但也许快速的代码示例可能有帮助...我显然是一个初学者。再次感谢! – user3708224 2014-08-27 18:30:40
你的表格单元数将取决于你的数组中的项目数量,所以是的,如果你的数组中有8个对象,你将有8个单元格。什么代码需要帮助? – 2014-08-27 20:08:10