我刚刚添加了scrollView视图,但我的应用程序崩溃了?

时间:2014-01-28 10:42:32

标签: ios uiscrollview

我有.h文件,如下所示

#import <UIKit/UIKit.h>

@interface EliteScreen : UIViewController

- (IBAction)homeBtnClk:(id)sender;

- (IBAction)eliteIBtnClk:(id)sender;

- (IBAction)eliteII_BtnClk:(id)sender;

@end

.m文件如下所示

#import "EliteScreen.h"
#import "EliteQuestionScreen.h"
#import "NextView.h"
#import "ScrollTestController.h"

@interface EliteScreen ()

@end

@implementation EliteScreen

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setfontForControll];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)addSubViewToScrollView {

    CGFloat x = 0;
    UIScrollView *scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    NSInteger viewcount= 8;

    for(int i = 0; i< viewcount; i++) {

        ScrollTestController* optimztionscreen = [[ScrollTestController alloc] initWithNibName:@"ScrollTestController" bundle:[NSBundle mainBundle]];

        if (i == 0) {
            x = optimztionscreen.view.frame.origin.x;
        } else {
            x = optimztionscreen.view.frame.size.width + x;
        }
        NSLog(@"I %d",i);

        UIView *viewMine = optimztionscreen.view;
        [viewMine setBackgroundColor:[UIColor redColor]];
        viewMine.frame = CGRectMake(x, 0, optimztionscreen.view.frame.size.width,
                                    optimztionscreen.view.frame.size.height);
        [scrollview setPagingEnabled:YES];
        [scrollview addSubview:viewMine];
    }
    scrollview.contentSize = CGSizeMake(self.view.frame.size.width *viewcount,
                                        self.view.frame.size.height);
    [self.view addSubview:scrollview];

    NSLog(@"MY VALUE FOR X %f",self.view.frame.size.width *viewcount);
}

- (IBAction)eliteII_BtnClk:(id)sender {
    [self addSubViewToScrollView];
}

- (IBAction)eliteIBtnClk:(id)sender {

}

@end

我的ScrollTestController.h文件如下

#import <UIKit/UIKit.h>

@interface ScrollTestController : UIViewController <UITableViewDelegate, UITableViewDataSource>

@end

我的ScrollTestController.m文件如下

#import "ScrollTestController.h"
#import "AppConstant.h"
#import "SimpleTableCell.h"
#import "AssetPopUpController.h"

@interface ScrollTestController ()

@end

@implementation ScrollTestController {
    NSArray *tableData;
    NSArray *thumbnails;
}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    tableData = [NSArray arrayWithObjects:first_screen_firstBtn_title,
                 first_screen_secondBtn_title,first_screen_thirdBtn_title, nil];

    thumbnails = [NSArray arrayWithObjects:@"assetImage_iPad.png",
                  @"assetImage_iPad.png",
                  @"assetImage_iPad.png",nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [tableData count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath {
    static NSString *simpleTableIdentifier = @"SimpleTableCell";

    SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    cell.nameLabel.text = [tableData objectAtIndex:indexPath.row];
    cell.thumbnailImageView.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]];

    [cell.nameLabel setFont:[UIFont fontWithName:@"UniversLTStd-BoldCn" size:13.00f]];
    [cell.nameLabel sizeToFit];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    SimpleTableCell *newCell = (SimpleTableCell *)[tableView cellForRowAtIndexPath:indexPath];

    NSString *Abc = newCell.nameLabel.text;
    NSLog(@"MY  TIME %@",Abc);

    AssetPopUpController* optimztionscreen = [[AssetPopUpController alloc] initWithNibName:@"AssetPopUpController" bundle:[NSBundle mainBundle]];
    [self.navigationController presentViewController:optimztionscreen animated:NO completion:nil];
    optimztionscreen.assetTitle.text = Abc;
    [optimztionscreen.assetTitle setFont:[UIFont fontWithName:@"UniversLTStd-BoldCn" size:20.00f]];
    [optimztionscreen.assetTitle sizeToFit];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 65;
}

@end

每当我点击EliteScreen中的精英II按钮时,我的应用就会崩溃 错误是:

****BussinessAdvantage[7118:11303] *** -[ScrollTestController tableView:numberOfRowsInSection:]: message sent to deallocated instance 0x28532cb0****

我在xib文件中使用了表格布局,并正确设置了它的数据源和delagates方法。

当我没有设置delagates和数据源时,它运行良好,但在设置数据源和转发后它会崩溃。

2 个答案:

答案 0 :(得分:0)

创建EliteScreen实例时,您要将其表视图添加到滚动视图,但不保留EliteScreen的实例。因此,当表视图尝试加载数据时,您会崩溃。

保留EliteScreen的实例以解决问题。

答案 1 :(得分:0)

为viewcontroller设置属性

@Property(nonatomic,retain)ScrollTestController* optimztionscreen;



self.optimztionscreen = 
         [[ScrollTestController alloc] initWithNibName:
         @"ScrollTestController" bundle:[NSBundle mainBundle]];