UITableView中的EXC_BAD_ACCESS错误

时间:2013-02-19 11:13:31

标签: objective-c uitableview exc-bad-access

我正在使用viewForFooterInSection,因为当我使用

tableView.tableFooterView = footerView;

我的按钮不可触摸。这就是为什么我想手动显示最后一部分的位置。

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {

   if(control.selectedSegmentIndex == 1 && section == 1)
   {
if(myFooterView == nil) {
    //allocate the view if it doesn't exist yet
    myFooterView  = [[UIView alloc] init];


    //create the button
    UIButton *footButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    [footButton setFrame:CGRectMake(110, 10, 100, 30)];

    //set title, font size and font color
    [footButton setTitle:@"Go Home" forState:UIControlStateNormal];
    [footButton.titleLabel setFont:[UIFont boldSystemFontOfSize:18]];

    //set action of the button
    [footButton addTarget:self action:@selector(clickPressed:)
         forControlEvents:UIControlEventTouchUpInside];

    //add the button to the view
    [myFooterView addSubview:footButton];
}

//return the view for the footer
return myFooterView;
   }
}

这行代码给出了EXC_BAD_ACCESS错误,我确信该部分存在:

if(control.selectedSegmentIndex == 1 && section == 1)

看不出有什么问题。

修改

@property (nonatomic, retain) UISegmentedControl *control;


- (void)viewWillAppear:(BOOL)animated
{
[self setTitle:_actor.actorNaam];
NSLog(@"Actor viewwillappear %@", _actor.actorNaam);

NSArray *items = [[NSArray alloc] initWithObjects: @"Actor", @"Object", @"Operation", @"Goal", nil];
control = [[UISegmentedControl alloc] initWithItems:items]; //!!!!!!!!!!!!!!!
control.selectedSegmentIndex = segmentPushed;
control.segmentedControlStyle = UISegmentedControlStylePlain;
control.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
control.apportionsSegmentWidthsByContent = YES;

1 个答案:

答案 0 :(得分:0)

假设您正在使用ARC环境,

您已将“控制”声明为保留但由于分配方式不当而未在ARC环境中保留。

进行更改:

control = [[UISegmentedControl alloc] initWithItems:items];

使用

self.control = [[UISegmentedControl alloc] initWithItems:items];

使用 self 将调用默认的setter,您的对象将被保留,此时此刻尚未完成。