将特定数组连接到UITableViewCell UIImage和UILabel?

时间:2014-06-11 17:47:37

标签: ios objective-c uitableview

不确定我的问题标题是否最具体,但我会尝试解释:

我有一个uitableviewscene,它会转移到另一个viewcontroller,它根据用户选择的项目显示数组。 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

1 个答案:

答案 0 :(得分: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;
}

你从来没有提到这个并且目前还没有使用它们,但我假设是因为你发布了自定义表格视图单元格的代码,而你想要使用带有你的标签和图像的代码。至于图像,您需要另一个在控制器中设置的静态数组(就像您使用allwxallintelallfuels所做的那样)或者您需要传入一个创建表视图控制器后,数组到您的属性,如下所示:

// 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;
}
相关问题