1

I create one program that show list of book from url in tableview (any book has many images) I want when to click any cell to go in next page (next page is UIScroll that show images) and show images of that book I have one problem that it is when to click any cell and when go next page show black screen instead UIScrollView include many images.

this is my code : RecipeViewController.h

#import "RecipeViewController.h"

@interface RecipeViewController : UITableViewController<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) IBOutlet UITableView *table;
@end

RecipeViewController.m

#import "RecipeViewController.h"
#import "Recipe.h"
#import "DetailViewController.h"

@implementation RecipeViewController
{
    NSMutableArray *recipes;
    NSInteger num;
}
@synthesize table;

- (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.100/mamal/book.php?all"]];

    NSInteger numbook = [numberbook integerValue];
    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.100/mamal/book.php?info=1&b=%d",i]]];        
        NSData *data = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://192.168.1.100/mamal/book.php?p=1&b=%d",i]]];
        si.name = [NSString stringWithString:c];
        si.imageFile = data;

        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;

}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    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 imageWithData:recipe.imageFile];

    UILabel *recipeNameLabel = (UILabel *)[cell viewWithTag:101];
    recipeNameLabel.text = recipe.name;

    return cell;
}
#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailViewController *obj = [[DetailViewController alloc]init];
    int x = (indexPath.row)+1;
    NSLog(@"x : %d",x);
    obj.yourValue = [NSString stringWithFormat:@"%d",(indexPath.row)+1];
    NSLog(@"yourValue1 : %@",obj.yourValue);
    obj.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentModalViewController:obj animated:YES];
}
@end

DetailViewController.h

#import <UIKit/UIKit.h>
#import "Recipe.h"
#import "RecipeViewController.h"

@interface DetailViewController : UIViewController<UIScrollViewDelegate>
{
    UIScrollView *scroller;
}
@property (nonatomic,strong) IBOutlet UIScrollView *scroller;
@property (nonatomic,strong) Recipe *rec;
@property (nonatomic,strong) NSString *yourValue;
@end

DetailViewController.m

#import "DetailViewController.h"

@implementation DetailViewController
@synthesize scroller,yourValue;

- (void)viewDidLoad
{
    [super viewDidLoad];
    int m1 = [self.yourValue integerValue];
   NSLog(@"M1 : %d",m1);

    NSString *bookID = [[NSString alloc]initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://192.168.1.100/mamal/book.php?info=0&b=%d",m1]]];

    NSInteger num1 = [bookID integerValue]; 
    NSLog(@"%d",num1);
    for (int i = 1; i <= num1; i++)
    {
        NSString *s = [NSString stringWithFormat:@"http://192.168.1.100/mamal/book.php?p=%d&b=%d",i,m1];
        NSData *dat = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:s]];
        NSLog(@"%@",s);
        UIImageView *imagen = [[UIImageView alloc] initWithImage:[UIImage imageWithData:dat]];
        imagen.frame = CGRectMake((i-1)*320, 0, 320, 460);
        [scroller addSubview:imagen];
    }
    scroller.delegate = self;
    scroller.contentSize = CGSizeMake(320*num1, 460);
    scroller.pagingEnabled = YES;  
}

@end
4

3 に答える 3

2

この問題を解決したところです。StoryBoard を使用している場合は、View Controller を次のように初期化する必要があります。

UIStoryboard *storybrd = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];

                storybrd = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
                YourViewController *newVC =[storybrd instantiateViewControllerWithIdentifier:@"YourVCId"];

識別子をストーリーボードに配置し、表示するビューを選択してから、ID インスペクターを選択する必要があります。必要な ID を書き込むオプションがあります。

于 2013-10-28T11:09:06.723 に答える
1

呼び出す必要があります: この時点では[[DetailViewController alloc]initWithNibName:@"nibName" bundle:nil];なく[[DetailViewController alloc]init]; 、インフレートする Xib がありません (init メソッドをオーバーライドしない限り)

于 2013-04-08T01:47:48.230 に答える