[英]add image from array to UIimageView in prototype Cell
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" 我已经建立了我的自定义原型单元(仅包含一个UIImageView),将标识符设置为“ thiscell”
here is my .h file: 这是我的.h文件:
@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: 因此,我开始编写将每个图像放入自定义原型单元中的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;
}
What do i need to put in to get the images into the UIImageView? 我需要放入什么才能将图像放入UIImageView? Also, there is a warning (indicated above) about local instance declaration - any suggestions on how to fix this?
此外,还有关于本地实例声明的警告(如上所示)-有关如何解决此问题的任何建议?
Thanks in advance 提前致谢
the ImageView needs to a property belonging to the UITableViewCell. ImageView需要一个属于UITableViewCell的属性。 When you reference the tableview cell afterwards in your view controller you would something like this :
之后,当您在视图控制器中引用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;
}
That should solve your problem. 那应该解决您的问题。
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.