2014-08-27 60 views
0

所以我想填充一個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 

回答

0

我不要將您的導航稱號的決定。將標題傳遞給您的詳細視圖控件。添加像這樣DetailViewController.h

@property(nonatomic,retain)NSString * selectedTitle;

在您的if語句中,檢查該值並執行您的條件。

更好的解決方案是創建表示數據的對象並將其傳遞給您的詳細視圖控制器。這將消除您的條件並使代碼更容易添加其他標題。

我會有一個對象的NSString的標題和NSArray的圖像顯示該位置。然後在你的父視圖控制器上,你將擁有一個擁有這些對象的NSArray,這將是你的表數據源。

如果你想更多地分離它,你可以創建一個類來管理你的數據,例如一個LocationRepository,你可以有一個方法來創建數組並填充你的數據,這樣任何視圖控制器都可以獲取這些數據。

+0

好的,謝謝。創建對象是有道理的。所以只是爲了澄清,如果我的tableViewController有8個單元格,我會創建8個NSObject類(標題)...然後使用父視圖控制器的實現文件中的NSArray圖像集創建每個對象的實例?不要挑剔,但也許快速的代碼示例可能有幫助...我顯然是一個初學者。再次感謝! – user3708224 2014-08-27 18:30:40

+0

你的表格單元數將取決於你的數組中的項目數量,所以是的,如果你的數組中有8個對象,你將有8個單元格。什麼代碼需要幫助? – 2014-08-27 20:08:10

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語句的問題。

給你的填充問題。在你的代碼中有一些用於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; 

}