1

我创建了一个程序,它在 tableview 中显示来自 url 的书列表(任何书都有很多图像)我想什么时候单击任何单元格进入下一页(下一页是显示图像的 UIScroll)并显示那本书的图像我有一个问题是何时单击任何单元格以及何时转到下一页显示黑屏而不是 UIScrollView 包含许多图像。

这是我的代码: RecipeViewController.h

#import "RecipeViewController.h"

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

食谱视图控制器.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

细节视图控制器.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,你必须以这种方式初始化视图控制器:

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

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

您必须将您的标识符放在情节提要中,选择您要显示的视图,然后选择身份检查器,您可以选择编写您想要的 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 回答
0

您根本没有调用 DetailViewController 如何显示图像。在 didSelectRowAtIndexPath 中尝试此操作:可能会对您有所帮助

DetailViewController *DVC = [[DetailViewController alloc] initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:DVC animated:YES];
于 2013-04-08T04:49:58.017 回答