2014-06-11 17 views
0

不知道如果我的問題的標題是最具體的,但我會盡力解釋:連接特定數組到UITableViewCell UIImage和UILabel?

我有塞格斯到基於該項目的用戶已經選擇顯示陣列的另一個視圖 - 控制一個uitableviewscene。 segue工作正常,但是,我不確定如何將每個特定數組鏈接到新視圖控制器上的UIImage和UILabel。下面我提供了視圖控制器的代碼。我沒有足夠的代表發佈目前應用程序的圖像。有人可以請告知如何在這裏進行?!



SecondTableViewCell.h

#import <UIKit/UIKit.h> 

@interface SecondTableViewCell : UITableViewCell 

@property (nonatomic, strong) IBOutlet UIImageView *secondImage; 
@property (nonatomic, strong) IBOutlet UILabel *secondLabel; 

@end 



SecondTableViewCell.m

#import "SecondTableViewCell.h" 

@implementation SecondTableViewCell 
@synthesize secondLabel = _secondLabel; 
@synthesize secondImage = _secondImage; 

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 
     // Initialization code 
    } 
    return self; 
} 

- (void)awakeFromNib 
{ 
    // Initialization code 
} 

- (void)setSelected:(BOOL)selected animated:(BOOL)animated 
{ 
    [super setSelected:selected animated:animated]; 

    // Configure the view for the selected state 
} 

@end 



WXMenuTableViewController.h

#import <UIKit/UIKit.h> 

@interface WXMenu_TableViewController : UITableViewController 

@property (nonatomic, weak) NSString *secondview; 
@property(nonatomic,strong) NSArray *SecondMenuText; 
@property(nonatomic,strong) NSArray *SecondMenuImage; 

@end 



WXMenuTableViewController.m

#import "WXMenu_TableViewController.h" 
#import "SecondTableViewCell.h" 



@interface WXMenu_TableViewController() 

@end 

@implementation WXMenu_TableViewController 

@synthesize SecondMenuImage = _SecondMenuImage; 
@synthesize SecondMenuText = _SecondMenuText; 

//Define array for weather products list 
    NSArray *allwx; 
    NSArray *allintel; 
    NSArray *allfuels; 

- (id)initWithStyle:(UITableViewStyle)style 
{ 
    self = [super initWithStyle:style]; 
    if (self) { 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    //Define the products list 

    allwx = [NSArray arrayWithObjects:@"Daily Weather", @"Fire Potential", 
      @"Multi-Media Briefing", @"Sig. Fire Potential",@"Seasonal Outlook", @"Fire Season Broadcast", nil]; 
    allintel = [NSArray arrayWithObjects:@"New IA", @"Current Fire", nil]; 
    allfuels = [NSArray arrayWithObjects:@"Current Fuels Status",@"Fuel Danger", nil]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Set table size to one section. 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    //When Root Menu items are clicked on previous VC, specific lists is displayed 
    if ([_secondview isEqualToString:@"Weather"]) { 
     _SecondMenuText = allwx; 
     return [_SecondMenuText count]; 
     } 
    else if ([_secondview isEqualToString:@"Intelligence"]) { 
     _SecondMenuText = allintel; 
     return [_SecondMenuText count]; 
     } 
    else if ([_secondview isEqualToString:@"Fuels"]) { 
     _SecondMenuText = allfuels; 
     return [_SecondMenuText count]; 
    } 
    return 1; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *tableID = @"MainCell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableID]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableID]; 
    } 
    if ([_secondview isEqualToString:@"Weather"]) { 
     _SecondMenuText = allwx; 
     cell.textLabel.text = [_SecondMenuText objectAtIndex:indexPath.row]; 
    } 
    else if ([_secondview isEqualToString:@"Intelligence"]) { 
     cell.textLabel.text = [allintel objectAtIndex:indexPath.row]; 
    } 
    else if ([_secondview isEqualToString:@"Fuels"]) { 
     cell.textLabel.text = [allfuels objectAtIndex:indexPath.row]; 
    } 

    return cell; 
} 

@end 
+0

什麼不與上面的代碼一起工作,或者發生了什麼不應該發生的事情? – Stonz2

+0

到新視圖控制器的segue工作得很好:它填充新視圖控制器以顯示基於用戶在前一個場景表上的選擇的數組的表格。現在的問題是我不知道如何將這些新表連接到tableviewcell(uilabel和uiimage)。我會想象它是用某種類型的if/else語句解決的,但是很難。 – 3722839

回答

0

首先,設置你的數據源數組中numberOfRowsInSection是一個可怕的想法。此方法可以經常調用,並且您冗餘地設置數據源。如果你打算根據似乎是外部設置的字符串獲得一個靜態源,請在viewDidLoad中執行此操作。這也將大大簡化您的numberOfRowsInSection方法。 (還有似乎沒有任何必要有_SecondMenuText@property,但我會告訴該幻燈片)

(void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    //Define the products list 

    allwx = [NSArray arrayWithObjects:@"Daily Weather", @"Fire Potential", @"Multi-Media Briefing", @"Sig. Fire Potential",@"Seasonal Outlook", @"Fire Season Broadcast", nil]; 
    allintel = [NSArray arrayWithObjects:@"New IA", @"Current Fire", nil]; 
    allfuels = [NSArray arrayWithObjects:@"Current Fuels Status",@"Fuel Danger", nil]; 

    if ([_secondview isEqualToString:@"Weather"]) { 
     _SecondMenuText = allwx; 
    } 
    else if ([_secondview isEqualToString:@"Intelligence"]) { 
     _SecondMenuText = allintel; 
    } 
    else if ([_secondview isEqualToString:@"Fuels"]) { 
     _SecondMenuText = allfuels; 
    } 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if(_SecondMenuText.count) 
     return _SecondMenuText.count; 
    return 1; 
} 

你從來不提這一點,目前沒有使用它們,但我猜想因爲您爲自定義表格視圖單元發佈了代碼,您希望將這些代碼與標籤和圖像一起使用。至於圖像,你需要另外一組靜態數組,在控制器中設置(就像你使用allwx,allintelallfuels一樣),或者在創建表視圖控制器後需要將數組傳遞給屬性,像這樣:

// wherever you create the controller, set it 
WXMenuTableViewController *tableController = [[WXMenuTableViewController alloc] initWithStyle:UITableViewStyleNormal]; 
tableController.SecondMenuImage = anArrayOfImages; 

我會假設你的圖像陣列具有相同數量的對象作爲你的文字陣列對於任何給定情況。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *tableID = @"MainCell"; // make sure this is whatever you set as your reuse ID in your custom cell xib 
    SecondTableViewCell *cell = (SecondTableViewCell *)[tableView dequeueReusableCellWithIdentifier:tableID]; 

    if(!cell) 
     cell = [[[NSBundle mainBundle] loadNibNamed:@"SecondTableViewCell" owner:self options:nil] objectAtIndex:0]; 

    cell.secondLabel.text = [_SecondMenuText objectAtIndex:indexPath.row]; 
    cell.secondImage.image = [_SecondMenuImage objectAtIndex:indexPath.row]; 

    return cell; 
} 
+0

這工作得很好。我真的很感謝你的時間。我是一名貿易氣象學家,在沒有太多經驗的情況下通過這些東西進行黑客攻擊。太感謝了。 – 3722839