2

Я создаю одну программу, которая отображает список книг с url в tableview (любая книга имеет много изображений) Я хочу, когда нужно щелкнуть любую ячейку, чтобы перейти на следующую страницу (следующая страница - это UIScroll, который показывает изображения) и показывать изображения этой книги У меня есть одна проблема, что именно когда нужно щелкнуть любую ячейку, и когда на следующей странице появится черный экран, вместо UIScrollView будет включено много изображений.использовать [self presentModalViewController: анимированные:] и черный экран

это мой код: 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 
+0

Откуда возникает представление DetailViewController? Ae вы делаете этот контроллер в xib или раскадровке? – rdelmar

ответ

1

Вам нужно позвонить: [[DetailViewController alloc]initWithNibName:@"nibName" bundle:nil];, а не просто [[DetailViewController alloc]init];, как на данный момент, не имеют XIb надуть (если вы не отменяют метод инициализации)

0

Вы не призывающие DetailViewController Как показывает изображение .Try это didSelectRowAtIndexPath: может помочь вам

DetailViewController *DVC = [[DetailViewController alloc] initWithNibName:nil bundle:nil]; 
[self.navigationController pushViewController:DVC animated:YES]; 
2

Я просто решить эту проблему, если вы используете раскадровку вы должны инициализировать контроллер представления так:

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

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

Вы должны поместить свой идентификатор в раскадровке, выбрав вид, который вы хотите показать, а затем выбирая инспектор удостоверений личности, там у вас есть возможность написать идентификатор, который вы хотите.

 Смежные вопросы

  • Нет связанных вопросов^_^