Objective c 将图像从阵列添加到原型单元中的UIimageView
我有一组这样声明的图像:Objective c 将图像从阵列添加到原型单元中的UIimageView,objective-c,uitableview,uiimageview,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"]]; [menu
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;
我需要输入什么才能将图像放入UIImageView?另外,上面还有一个关于本地实例声明的警告——有没有关于如何修复此问题的建议
提前感谢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;
}
这应该可以解决您的问题。ibuitableview*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的.h或.m,你能把它贴出来吗。。。我没有?这是我第一次进入TableView…你需要重写UITableViewCell类。你能提供一个基于上述代码的示例吗?你找到了一个图图如果你有一个原型单元格,就为它创建一个类。在类中创建属性backgroundImageView。然后可以在CellForRowatineXpath中使用自定义tableviewcell类。有很多关于创建自定义tableview单元格的好教程。试着用谷歌搜索一下。我已经实现了上面的解决方案,它确实有效。但是,我放在prototype单元格中的UIImageView占据了行的整个宽度,但图像没有。有什么想法吗?这是因为行UITableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@thiscell];这里发生的事情是,您正在创建UITableViewCell的对象,它带有默认的imageView属性。您需要为prototype cell指定一个类,并将该调用出列以代替UITableViewCell。第二个解决方案是。阅读本教程,您会找到答案。本教程没有提供有关调整tableview单元格的UIImageView大小的信息…@scb998调整UIImageView大小是什么意思,为什么需要这样做。我建议您阅读与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;
}