if语句用于带有tableview的detailedViewController

时间:2014-08-27 14:05:00

标签: objective-c uitableview ios7 xcode5

所以我试图填充从另一个tableviewController推送的UIViewController中的tableview。我知道我必须写一个if语句,但我遇到了麻烦...重申一下我想我的问题是你如何在tableViewController推送的detailedViewController中填充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

2 个答案:

答案 0 :(得分:0)

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语句的问题。

填写问题。在你的代码中有一些_Title.count用于numberOfRowsInSection。我不知道那是什么类型的数组,但是:

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

}

答案 1 :(得分:0)

我不会根据导航标题做出决定。将标题传递给详细视图控件。将这样的内容添加到DetailViewController.h

@property(nonatomic,retain)NSString * selectedTitle;

在if语句中,检查该值并执行条件。

更好的解决方案是创建表示数据的对象并将其传递给详细视图控制器。这将消除您的条件并使代码更容易添加其他标题。

我会有一个对象有一个NSString用于标题,一个NSArray用于显示该位置的图像。然后在您的父视图控制器上,您将拥有一个NSArray,其中包含每个对象,这将是您的表数据源。

如果你想要将它分开,你可以创建一个为你管理数据的类,例如一个LocationRepository,你可以有一个创建数组并填充数据的方法,这样任何视图控制器都可以获得那个数据。