由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,从-traitCollection返回nil

时间:2019-11-26 13:36:59

标签: ios objective-c uicollectionview

在模拟器中运行我的应用程序,出现此错误:

由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'从-traitCollection返回nil,这是不允许的。'

它建于3年前,目前在IOS应用商店中,如果我下载该应用,则可以正常运行。

我累了要插入

[collectionView_ reloadData];
[collectionView_.collectionViewLayout invalidateLayout];

它将在

停止
int main(int argc, char * argv[]) {
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}

具有线程1:信号SIGABRT

我搜索了它可能会导致 1.我的应用程序不知道从哪里开始。 ->我应该在AppDelegate中设置它。

这是代码。 [Appdelegate]

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    [GMSServices provideAPIKey:GOOGLEMAP_KEY];

    self.locationManager = [[CLLocationManager alloc] init];

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
        // 앱이 활성화 되어 있는 동안에만 위치서비스를 이용하는 경우
        [self.locationManager requestAlwaysAuthorization];
    }

    [self setUpLocationService];
    [self setPushService];

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_8_0
    if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
        UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound) categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    } else {
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
    }
#else
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
#endif

    return YES;
}

[CardBenefitViewController.m]

//
//  CardBenefitViewController.m


#import "CardBenefitViewController.h"
#import <AFHTTPSessionManager.h>
#import "ReDefine.h"
#import "APIUtils.h"
#import "EventObj.h"
#import <UIImageView+AFNetworking.h>
#import "CardEventCell.h"
#import "ReUserDefaults.h"
#import "NoticeViewController.h"
#import "CardPickDetailViewController.h"
#import "CardWebViewController.h"
#import "ReUtil.h"

#define CELLID  @"Event_List_Cell"
#define CARD_EVENT_CELL @"CardEventCell"


@interface CardBenefitViewController () <UITableViewDelegate, UITableViewDataSource, UICollectionViewDelegate, UICollectionViewDataSource> {
    CTabView *tabView_;
    NSArray *eventList_;
    NSArray *cardPicks_;
    NSArray *eventCards_;

    NSInteger selectedTabIndex_;
    UICollectionView *collectionView_;
    UIPageControl *pageControl_;
}
@property (nonatomic, weak) IBOutlet UITableView *tableView;


@end

@implementation CardBenefitViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self initView];

    selectedTabIndex_ = 0;


}

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

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    dispatch_async(dispatch_get_main_queue(), ^{
        if ([[ReUserDefaults getUserCardList] count] > 0) {
            [self reqEventSortSwipe];
            [self reqEventCardList];
        }
    });

}

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.

    if ([segue.identifier isEqualToString:@"noticeSeg"]) {
        NoticeViewController *controller = (NoticeViewController *)[segue destinationViewController];
        [controller setStrUrl:sender];
    }
    else if ([segue.identifier isEqualToString:@"cardPickDetailSeg"]) {
        CardPickDetailViewController *controller = (CardPickDetailViewController *)[segue destinationViewController];
        [controller setCardPicks:sender];
    }
}



- (void)initView {
    tabView_ = [[CTabView alloc] initWithWidth:SCREENSIZE_WIDTH titleArray:[NSArray arrayWithObjects:@"카드PICK", @"카드사 이벤트", nil]];
    [tabView_ setDelegate:self];
    [tabView_ setFrame:CGRectMake(0, 0, SCREENSIZE_WIDTH, 44)];
    [tabView_ setSelectTabIndex:selectedTabIndex_];
    [self.view addSubview:tabView_];

    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:CELLID];
}

- (UIView *)setTableViewHeader {

    CGFloat headerHeight = 194;
    if ([ReUtil isIPhone6Screen]) {
        headerHeight = 224;
    }

    if ([ReUtil isIphone6PlusScreen]) {
        headerHeight = 234;
    }


    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 44, SCREENSIZE_WIDTH, headerHeight)];
    [header setBackgroundColor:[UIColor clearColor]];

    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    [layout setMinimumLineSpacing:0];
    [layout setMinimumInteritemSpacing:0];

    CGFloat height = 170;

    if ([ReUtil isIPhone6Screen]) {
        height = 200;
    }

    if ([ReUtil isIphone6PlusScreen]) {
        height = 210;
    }



    collectionView_ = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREENSIZE_WIDTH, height) collectionViewLayout:layout];
    [collectionView_ setBackgroundColor:[UIColor clearColor]];
    [collectionView_ setDelegate:self];
    [collectionView_ setDataSource:self];
    [collectionView_ setShowsHorizontalScrollIndicator:NO];
    [collectionView_ setPagingEnabled:YES];
    [collectionView_ setBounces:NO];
    [collectionView_ registerClass:[UICollectionViewCell class]
        forCellWithReuseIdentifier:@"SwipeCell"];

    //여기 고침
    [collectionView_ reloadData];
    [collectionView_.collectionViewLayout invalidateLayout];
    [header addSubview:collectionView_];

    pageControl_ = [[UIPageControl alloc] initWithFrame:CGRectMake((SCREENSIZE_WIDTH - pageControl_.frame.size.width)/2, header.frame.size.height - 17, 10, 15)];
    [pageControl_ setCurrentPage:0];
    [pageControl_ setNumberOfPages:[cardPicks_ count]];
    [pageControl_ setPageIndicatorTintColor:COLOR04];
    [pageControl_ setCurrentPageIndicatorTintColor:COLOR03];
    pageControl_.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin;
    [header addSubview:pageControl_];

    return header;
}

- (void)reqEventSortSwipe {
    NSArray *cardNum = [ReUserDefaults getUserCardList];

    [APIUtils eventSortSwipeWithCardId:cardNum gubun_new:@"all" completion:^(NSDictionary *json) {

        NSMutableArray *arr = [NSMutableArray new];
        for (NSDictionary *dic in [json objectForKey:@"items"]) {
            EventObj *obj = [[EventObj alloc] initWithData:dic];
            [arr addObject:obj];
        }

        if ([arr count] > 0) {
            cardPicks_ = [NSArray arrayWithArray:arr];

            if (selectedTabIndex_ == 0) {
                self.tableView.tableHeaderView = [self setTableViewHeader];
            }
        }

        [self reqEventSortList];
    }];
}


- (void)reqEventSortList {
    NSArray *cardNum = [ReUserDefaults getUserCardList];

    [APIUtils eventSortListWithCardId:cardNum gubun_new:@"all" completion:^(NSDictionary *json) {

        NSMutableArray *arr = [NSMutableArray new];
        for (NSDictionary *dic in [json objectForKey:@"items"]) {
            EventObj *obj = [[EventObj alloc] initWithData:dic];
            [arr addObject:obj];
        }

        eventList_ = [NSArray arrayWithArray:arr];

        [collectionView_ reloadData];
        [self.tableView reloadData];
    }];
}

- (void)reqEventCardList {
    NSArray *cards = [ReUserDefaults getUserCardList];

    NSMutableArray *arr = [NSMutableArray new];
    for (NSDictionary *dic in cards) {
        NSNumber *num = [dic objectForKey:@"cardId"];
        [arr addObject:num];
    }

    [APIUtils eventCardCorpWithCardIds:arr completion:^(NSDictionary *json) {
        NSMutableArray *my_card_corps = [NSMutableArray new];
        NSMutableArray *card_corps = [NSMutableArray new];
        for (NSDictionary *dic in [json objectForKey:@"my_card_corps"]) {
            EventCardObj *obj = [[EventCardObj alloc] initWithData:dic];
            [my_card_corps addObject:obj];
        }

        for (NSDictionary *dic in [json objectForKey:@"card_corps"]) {
            EventCardObj *obj = [[EventCardObj alloc] initWithData:dic];
            [card_corps addObject:obj];
        }

        eventCards_ = @[my_card_corps, card_corps];
    }];
}


- (void)didSelectTabButtonIndex:(NSInteger)index {
    NSLog(@"index:%li", (long)index);

    if (index == 1) {
        [self.tableView.tableHeaderView removeFromSuperview];
        self.tableView.tableHeaderView = nil;
    }
    else {
        self.tableView.tableHeaderView = [self setTableViewHeader];
    }

    selectedTabIndex_ = index;

    [self.tableView reloadData];
}


#pragma mark - collectionView delegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    [self.tableView reloadData];

    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return [cardPicks_ count];;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

    CGFloat height = 170;

    if ([ReUtil isIPhone6Screen]) {
        height = 200;
    }

    if ([ReUtil isIphone6PlusScreen]) {
        height = 210;
    }

    CGSize size = CGSizeMake(SCREENSIZE_WIDTH, height);

    return size;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SwipeCell" forIndexPath:indexPath];
    [cell setBackgroundColor:[UIColor clearColor]];

    if (cell == nil) {
        cell = [[UICollectionViewCell alloc] initWithFrame:CGRectMake(0, 0, SCREENSIZE_WIDTH, 170)];
    }
    else {
        for(UIView *view in cell.subviews){
            if ([view isKindOfClass:[UIView class]]) {
                [view removeFromSuperview];
            }
        }
    }

    if ([cardPicks_ count] > 0) {
        EventObj *obj = [cardPicks_ objectAtIndex:indexPath.row];

        UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height)];
        [iv setClipsToBounds:YES];
        NSURL *imgUrl = [NSURL URLWithString:obj.img_url_swipe];
        [iv setImageWithURL:imgUrl placeholderImage:nil];

        [cell addSubview:iv];
    }


    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if ([cardPicks_ count] > 0) {
        EventObj *obj = [cardPicks_ objectAtIndex:indexPath.row];

        [APIUtils detailEventWithEventId:obj.event_id completion:^(NSDictionary *json){
            NSString *str = [json objectForKey:@"detail_img_01"];
            [self performSegueWithIdentifier:@"noticeSeg" sender:str];
        }];
    }
}


#pragma mark - tableview delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat height = 45;

    if (selectedTabIndex_ == 0) {
        height = 183;

        if ([ReUtil isIPhone6Screen]) {
            height = 215;
        }

        if ([ReUtil isIphone6PlusScreen]) {
            height = 230;
        }
    }

    return height;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (selectedTabIndex_ == 1) {
        return [eventCards_ count];
    }
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (selectedTabIndex_ == 1) {
        return [[eventCards_ objectAtIndex:section] count];
    }
    return [eventList_ count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (selectedTabIndex_ == 1) {
        return 40;
    }

    return 0;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = nil;
    if (selectedTabIndex_ == 1) {
        view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREENSIZE_WIDTH, 40)];
        [view setBackgroundColor:RGBX(0xf7f7f7)];

        UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(15, 14, SCREENSIZE_WIDTH - 50, 17)];
        [lbl setFont:FONT_SIZE(13)];
        [lbl setTextColor:COLOR07];

        NSString *sectionTitle = @"";
        if (section == 0) {
            sectionTitle = @"등록된 카드사";
        }
        else {
            sectionTitle = @"모든 카드사";
        }
        [lbl setText:sectionTitle];
        [view addSubview:lbl];

        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(view.frame) - 1, SCREENSIZE_WIDTH, 1)];
        [line setBackgroundColor:COLOR04];
        [view addSubview:line];
    }

    return view;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    if (selectedTabIndex_ == 0) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CELLID];
        [cell setBackgroundColor:[UIColor clearColor]];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

        for (UIView *view in cell.contentView.subviews) {
            [view removeFromSuperview];
        }

        if ([eventList_ count] > 0 && selectedTabIndex_ == 0) {
            EventObj *obj = [eventList_ objectAtIndex:indexPath.row];
            CGFloat height = 183;

            if ([ReUtil isIPhone6Screen]) {
                height = 215;
            }

            if ([ReUtil isIphone6PlusScreen]) {
                height = 230;
            }

            UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(15, 0, SCREENSIZE_WIDTH - 30, height)];
            [iv setContentMode:UIViewContentModeScaleAspectFill];
            [iv setClipsToBounds:YES];
            NSURL *imgUrl = [NSURL URLWithString:obj.img_url_list];
            [iv setImageWithURL:imgUrl placeholderImage:nil];
            [cell.contentView addSubview:iv];
        }

        return cell;
    }
    else {
        CardEventCell *cell = [tableView dequeueReusableCellWithIdentifier:CARD_EVENT_CELL forIndexPath:indexPath];
        [cell setBackgroundColor:[UIColor clearColor]];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

        UIView *line = [[UIView alloc] init];

        if (indexPath.row == [[eventCards_ objectAtIndex:indexPath.section] count] - 1) {
            [line setFrame:CGRectMake(0, 44, SCREENSIZE_WIDTH - CGRectGetMidX(cell.lblCardName.frame), 1)];
        }
        else {
            [line setFrame:CGRectMake(93, 44, SCREENSIZE_WIDTH - 93, 1)];
        }

        [line setBackgroundColor:COLOR04];

        [cell.contentView addSubview:line];

        if ([[eventCards_ objectAtIndex:indexPath.section] count] > 0) {
            EventCardObj *obj = [[eventCards_ objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
            [cell.lblCardName setText:obj.name];
            NSURL *imgUrl = [NSURL URLWithString:obj.icon_url];
            [cell.ivCard setContentMode:UIViewContentModeScaleAspectFit];
            [cell.ivCard setClipsToBounds:YES];
            [cell.ivCard setImageWithURL:imgUrl placeholderImage:nil];
        }

        [cell setNeedsLayout];

        return cell;
    }

    return nil;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (selectedTabIndex_ == 0) {
        EventObj *obj = [eventList_ objectAtIndex:indexPath.row];
        NSLog(@"event_id:%@", obj.event_id);

        [APIUtils detailEventWithEventId:obj.event_id completion:^(NSDictionary *json){
            NSMutableArray *arr = [NSMutableArray new];
            for (NSDictionary *dic in [json objectForKey:@"cards"]) {
                CardPickInfo *obj = [[CardPickInfo alloc] initWithData:dic];

                [arr addObject:obj];
            }

            UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            CardPickDetailViewController *controller = (CardPickDetailViewController *)[storyBoard instantiateViewControllerWithIdentifier:@"CardPickDetailViewController"];
            [controller setCardPicks:arr];
            [controller setStrUrl:[json objectForKey:@"detail_img_01"]];

            [self.navigationController pushViewController:controller animated:YES];

        }];
    }
    else {
        EventCardObj *obj = [[eventCards_ objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];

        UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        UINavigationController *navController = [storyBoard instantiateViewControllerWithIdentifier:@"cardWebNav"];
        CardWebViewController *controller = (CardWebViewController *)navController.topViewController;
        NSURL *url = [NSURL URLWithString:obj.link_url];

        [controller setNavTitle:obj.name];
        [controller setUrl:url];

        [self presentViewController:navController animated:YES completion:^{

        }];
    }
}



- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView == collectionView_) {
        CGFloat width = scrollView.frame.size.width;
        NSInteger currentPage = (scrollView.contentOffset.x + (0.5f * width)) / width;

        [pageControl_ setCurrentPage:currentPage];
    }
}

@end

0 个答案:

没有答案