objective-c中indexPath.row的EXC_BAD_ACCESS错误

时间:2011-12-13 12:08:10

标签: objective-c exc-bad-access

**Consider this code:**

        - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier index : (NSIndexPath*)index{
            if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {

                [self setSelectionStyle:UITableViewCellSelectionStyleNone];
                // Initialization code
                primarylabel=[[UILabel alloc] init];
                primarylabel.textAlignment = UITextAlignmentLeft;
                primarylabel.font = [UIFont boldSystemFontOfSize:14];
                primarylabel.numberOfLines=0;

                descLabel = [[UILabel alloc]init];
                descLabel.textAlignment = UITextAlignmentLeft;
                descLabel.font = [UIFont systemFontOfSize:11];
                descLabel.numberOfLines=0;


                secondarylabel = [[UIButton alloc]init];
                [secondarylabel.titleLabel setFont:[UIFont systemFontOfSize:16]];
                [self.contentView addSubview:primarylabel];
                [self.contentView addSubview:secondarylabel];
                [self.contentView addSubview:descLabel];
                rowIndex = index;
                switchButton = [[UISwitch alloc] initWithFrame:CGRectZero];

                [self.contentView addSubview:switchButton];

                segmentController = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Mon",@"Sun", nil]];
                [self.contentView addSubview:segmentController];
                [segmentController addTarget:self action:@selector(valueChanged:) forControlEvents:UIControlEventValueChanged];

                NSUserDefaults* standardUserDefaults = [NSUserDefaults standardUserDefaults];
                NSNumber *num1 =[standardUserDefaults valueForKey:DAY_SETTINGS];
                [segmentController setSelectedSegmentIndex:[num1 intValue]];
                //myLabel.font = [UIFont boldSytemFontofSize:16];                   
            }
  return self;
}



// THE ERROR IS GENERATING HERE

    - (void)layoutSubviews {
        [super layoutSubviews];
        CGRect frame;
        frame= CGRectMake(20 ,10, 250, 20);
        primarylabel.frame = frame;
        frame= CGRectMake(15 ,30, 250, 40);
        descLabel.frame = frame;    

    NSLog(@"*****rowIndex.row###### %d", rowIndex.row);// Here i am getting value but in IF CONDITION IT DOES NOT WORK

/* IF I OFF THIS CODE from HERE THEN IT WORKING */
        if(rowIndex.row == 0){ // I think problem is here
            NSLog(@"*****rowIndex.row###### %d", rowIndex.row);
            [segmentController setHidden:TRUE];
            CGRect frame= CGRectMake(195 ,20,50, 25);
            switchButton.frame = frame;
        }   
        if(rowIndex.row ==1)
        {
            [switchButton setHidden:TRUE];
            CGRect frame=CGRectMake(195 ,20,95,30);
            [segmentController setTitle:@"Man" forSegmentAtIndex:0];
            [segmentController setTitle:@"Søn" forSegmentAtIndex:1];
            segmentController.frame  = frame;       
        }
    }   

### THE ERROR IS GENEREATE LIKE BELLOW
2011-12-13 17:45:53.945 CalenderApp[1993:16a03] *****rowIndex.row###### 0
2011-12-13 17:45:53.945 CalenderApp[1993:16a03] *****rowIndex.row###### 0
2011-12-13 17:45:53.948 CalenderApp[1993:16a03] *****rowIndex.row###### 1
sharedlibrary apply-load-rules all
(gdb) bt
#0  0x01f6c09b in objc_msgSend ()
#1  0xbfffd4b8 in ?? ()
#2  0x00425322 in -[UIView(CALayerDelegate) layoutSublayersOfLayer:] ()
#3  0x0226fe72 in -[NSObject performSelector:withObject:] ()
#4  0x010ba92d in -[CALayer layoutSublayers] ()
#5  0x010c4827 in CA::Layer::layout_if_needed ()
#6  0x0104afa7 in CA::Context::commit_transaction ()
#7  0x0104cea6 in CA::Transaction::commit ()
#8  0x0104c580 in CA::Transaction::observer_callback ()
#9  0x022429ce in __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ ()
#10 0x021d9670 in __CFRunLoopDoObservers ()
#11 0x021a54f6 in __CFRunLoopRun ()
#12 0x021a4db4 in CFRunLoopRunSpecific ()
#13 0x021a4ccb in CFRunLoopRunInMode ()
#14 0x02670879 in GSEventRunModal ()
#15 0x0267093e in GSEventRun ()
#16 0x003e6a9b in UIApplicationMain ()
#17 0x00002e7d in main (argc=1, argv=0xbffff5b0) at /Volumes/Daily work/Today Job/Danish_CalenderApp 13_12_2010 region/main.m:14
Current language:  auto; currently objective-c

现在我如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

rowIndex = index;更改为self.rowIndex = index;