我想在表格视图中创建一个页面。它显示4个单元格,表示任何单元格的不同图像和名称。 (此信息在循环中获取URL)
首先:我创建了一个我可以理解为2变量的类。
Recipe.h #import
@interface Recipe : NSObject
@property (nonatomic, strong) NSString *name; // name of recipe
@property (nonatomic, strong) NSString *imageFile; // image filename of recipe
@end so now in RecipeViewController (root) I write this code :
#import "RecipeViewController.h"
#import "Recipe.h"
@interface RecipeViewController ()
{
IBOutlet UIImageView *ab;
}
@end
@implementation RecipeViewController
{
NSMutableArray *recipes;
NSInteger num;
}
@synthsize ab;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.title = @"Recipe Book";
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:nil
action:nil];
[[self navigationItem] setBackBarButtonItem:backButton];
NSString *numberbook = [[NSString alloc]initWithContentsOfURL:[NSURL
URLWithString:@"http://192.168.1.102/mamal/book.php?all"]];
NSInteger numbook = [numberbook integerValue];
NSMutableArray *b = [[NSMutableArray alloc]initWithCapacity:numbook];
for (int i = 1; i <= numbook; i++)
{
Recipe *si = [Recipe new];
NSLog(@"%d,%@",i,si);
NSString *c = [[NSString alloc]initWithContentsOfURL:[NSURL URLWithString:[NSString
stringWithFormat:@"http://192.168.1.102/mamal/book.php?info=1&b=%d",i]]];
NSString *a = [NSString stringWithFormat:@"http://192.168.1.102/mamal/book.php?p=1&b=%d",i];
[b addObject:a];
UIImage *myImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:a]]];
[ab setImage:myImage];
NSLog(@"%@",a);
NSLog(@"%@",c);
si.name = [NSString stringWithString:c];
si.imageFile =[]; //I so confused!!!
if(!recipes){
recipes = [NSMutableArray array];
}
[recipes addObject:si];
}
num = numbook;
// Remove table cell separator
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
// Assign our own backgroud for the view
self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"common_bg"]];
self.tableView.backgroundColor = [UIColor clearColor];
// Add padding to the top of the table view
UIEdgeInsets inset = UIEdgeInsetsMake(5, 0, 0, 0);
self.tableView.contentInset = inset;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return recipes.count;
}
- (UIImage *)cellBackgroundForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger rowCount = [self tableView:[self tableView] numberOfRowsInSection:0];
NSInteger rowIndex = indexPath.row;
UIImage *background = nil;
if (rowIndex == 0) {
background = [UIImage imageNamed:@"cell_top.png"];
} else if (rowIndex == rowCount - 1) {
background = [UIImage imageNamed:@"cell_bottom.png"];
} else {
background = [UIImage imageNamed:@"cell_middle.png"];
}
return background;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
// Display recipe in the table cell
Recipe *recipe = [recipes objectAtIndex:indexPath.row];
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:recipe.imageFile];
UILabel *recipeNameLabel = (UILabel *)[cell viewWithTag:101];
recipeNameLabel.text = recipe.name;
UILabel *recipeDetailLabel = (UILabel *)[cell viewWithTag:102];
recipeDetailLabel.text = recipe.detail;
// Assign our own background image for the cell
UIImage *background = [self cellBackgroundForRowAtIndexPath:indexPath];
UIImageView *cellBackgroundView = [[UIImageView alloc] initWithImage:background];
cellBackgroundView.image = background;
cell.backgroundView = cellBackgroundView;
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
@end
我很困惑并且得到了这个错误:
“连接不能将原型对象作为目标”
我也不知道在单元格中显示图像的一种方法!!!
答案 0 :(得分:0)
cell.imageView.image = yourImage
答案 1 :(得分:0)
试试这个
cell.yourImage.image = [UIImage imageNamed:@"imageName"];