2013-03-28 2 views
0

Я хочу создать одну страницу в виде таблицы. его дисплей 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

UIImage * фоновую = ноль; заменить на UIImage * background = [[UIImage alloc] init]; –

+0

http://stackoverflow.com/questions/12150715/strange-error-when-adding-items-to-prototype-cells-in-storyboard-ib и http://stackoverflow.com/questions/13381002/connect-activity -indicator к прототипам-клеток – iPatel

ответ

0

Попробуйте

cell.yourImage.image = [UIImage imageNamed:@"imageName"]; 
Смежные вопросы