0

I have an array of images declared as such:

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"]];

I've then determined the length of the table required like so:

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

Ive built my custom prototype cell (containing only one UIImageView), set the identifier to "thiscell"

here is my .h file:

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

so I've started writing the method to place each image into the UIImageView in the custom Prototype Cell, as below:

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

What do i need to put in to get the images into the UIImageView? Also, there is a warning (indicated above) about local instance declaration - any suggestions on how to fix this?

Thanks in advance

P.J.Radadiya
  • 1,493
  • 1
  • 12
  • 21
scb998
  • 899
  • 5
  • 18
  • 42

2 Answers2

-1

the ImageView needs to a property belonging to the UITableViewCell. When you reference the tableview cell afterwards in your view controller you would something like this :

@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;  
}

That should solve your problem.

-1

IBOutlet UITableView *tableView; was causing the warning, move it its proper place like below.

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

// for showing images in table view

-(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;
}
P.J.Radadiya
  • 1,493
  • 1
  • 12
  • 21
Pawan Rai
  • 3,434
  • 4
  • 32
  • 42
  • if you have a prototype cell, make a class for it. make a property backgroundImageView in class. then you can use the custom tableviewcell class in cellForRowAtIndexPath. there are so many good tutorials about making a custom tableview cell. try to google it. [link1](http://stackoverflow.com/questions/9245969/in-a-storyboard-how-to-make-a-custom-cell-for-use-with-multiple-controllers) – Pawan Rai Aug 26 '14 at 06:22
  • I have implemented the solution above, and it does work. however, the UIImageView I placed in the prototype cell takes up the entire width of the row, but the image doesn't. Any ideas? – scb998 Aug 26 '14 at 06:23
  • its happening because of this line UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: @"thiscell"]; what happens here is you are making object of UITableViewCell which comes with default imageView property. you need to give a class for prototype cell and dequeue that call in place of UITableViewCell. second solution is [here](http://code.tutsplus.com/tutorials/ios-sdk-crafting-custom-uitableview-cells--mobile-15702) . read it you will find your answer. – Pawan Rai Aug 26 '14 at 06:28
  • The tutorial does not provide information on re-sizing the UIImageView of the tableview cell... – scb998 Aug 26 '14 at 06:37
  • @scb998 what do you mean by re-sizing the UIImageView, why even you need this. i will suggest you to read tutorial related to tableview. its really a very simple task what you are asking. – Pawan Rai Aug 26 '14 at 06:53
  • @scb998 here is a good tutorial to do this [Customize TableViewCells with Storyboards](http://www.ioscreator.com/tutorials/customize-tableviewcells-with-storyboards) – Pawan Rai Aug 26 '14 at 06:55
  • Thanks for that tutorial, it helped. How do i set the delegates etc? the tutorial is basing it on a full table view controller, where my table view is embedded in another view? – scb998 Aug 27 '14 at 00:51