在原型Cell中将数组从数组添加到UIimageView

时间:2014-08-26 05:35:55

标签: objective-c uitableview uiimageview

我有一个声明如下的图像数组:

menuArray = [[NSMutableArray alloc] init];
[menuArray addObject:[UIImage imageNamed:@"one image.png"]];
[menuArray addObject:[UIImage imageNamed:@"another image.png"]];
[menuArray addObject:[UIImage imageNamed:@"and another.png"]];
[menuArray addObject:[UIImage imageNamed:@"and one more.png"]];
然后,我确定了所需的桌子长度,如下:

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [menuArray count];
}

我已经构建了我的自定义原型单元格(只包含一个UIImageView),将标识符设置为" thiscell"

这是我的.h文件:

@interface RoadSafetyAppViewController : UIViewController <MFMailComposeViewControllerDelegate, UITableViewDelegate, UITableViewDataSource>{
    IBOutlet UITableView *tableView;
    NSMutableArray *menuArray;
}
@property (strong, nonatomic) IBOutlet UIImageView *backgroundImage;
@property (strong, nonatomic) UIApplication *application;

所以我开始编写方法将每个图像放入自定义Prototype Cell中的UIImageView中,如下所示:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: @"thiscell"];// warning here about local declaration of 'tableView' hides instance variable
    //what do i put in here?
    return cell;
}

我需要输入什么才能将图像放入UIImageView?此外,还有一个关于本地实例声明的警告(如上所示) - 有关如何解决此问题的任何建议吗?

提前致谢

2 个答案:

答案 0 :(得分:-1)

ImageView需要属于UITableViewCell的属性。之后在视图控制器中引用tableview单元格时,您会这样:

@interface RoadSafetyAppViewController : UITableViewCell
@property (strong, nonatomic) IBOutlet UIImageView *backgroundImageView; //<- in your MyCustomTableViewCell Class with outlet to the storyboard

//in your view controllers .m file change the method like this

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{  
    MyCustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: @"cell"];  
    // warning here about local declaration of 'tableView' hides instance variable  
    //what do i put in here? <- perhaps a different name would solve this, or use a UITableViewController which has a sort of built in 'tableview' variable
    cell.backgroundImageView.image = menuArray[indexpath.row];  
    return cell;  
}

这应该可以解决你的问题。

答案 1 :(得分:-1)

IBOutlet UITableView *tableView;导致警告,将其移至正确位置,如下所示。

@interface RoadSafetyAppViewController : UIViewController <MFMailComposeViewControllerDelegate, UITableViewDelegate, UITableViewDataSource>
{
    NSMutableArray *menuArray;
}

@property (weak,   nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) IBOutlet UIImageView *backgroundImage;
@property (strong, nonatomic) UIApplication *application;

//用于在表格视图中显示图像

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: @"thiscell"];// 
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"thiscell"];
    }
    cell.imageView.image = menuArray[indexpath.row]; 
    return cell;
}