【问题标题】:Bad Access error on long tap on section of UItableview [closed]长按 UItableview 部分时出现错误访问错误 [关闭]
【发布时间】:2014-09-19 09:27:19
【问题描述】:

这是我的代码。我用它来显示表格部分的内容并在点击部分时重新加载

    -(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    NSArray *arr=[[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
    CustomCell *cellHeader=[arr objectAtIndex:0];
   UIButton *button = [[UIButton alloc] initWithFrame: CGRectMake(0.0, 0.0, 320.0, 80.0)];
    button.alpha = 0.7;
    button.tag=section;
    /* Prepare target-action */
    [button addTarget: self action: @selector(handleTap:) forControlEvents: UIControlEventTouchUpInside];
    [cellHeader.contentView addSubview: button];
    objISLMenu= [self.dataArray objectAtIndex:section];
    NSString *titleStr= objISLMenu.displayName;
    cellHeader.titleLabel.text=titleStr;
    if(section ==selectedSection){
        //bg as red
        [cellHeader.contentView setBackgroundColor:[UIColor redColor]];
        //text as white
        [cellHeader.titleLabel setTextColor:[UIColor greenColor]];
    }
    else{
        //bg as white
        [cellHeader.contentView setBackgroundColor:[UIColor redColor]];
        //text as white
        [cellHeader.titleLabel setTextColor:[UIColor greenColor]];
    }
    return cellHeader.contentView;
    }

-(void)handleTap:(UIButton*)sender
{
NSLog(@"%ld",(long)sender.tag);
if(selectedSection==sender.tag){
    //clicked is same as expanded
    selectedSection=-1;
    [self.tableview reloadSections:[NSIndexSet indexSetWithIndex:sender.tag] withRowAnimation:UITableViewRowAnimationNone];
}
else{
    NSLog(@"%ld",(long)sender.tag);
    long removesectionid=selectedSection;
    selectedSection=sender.tag;
    [self.tableview reloadSections:[NSIndexSet indexSetWithIndex:sender.tag] withRowAnimation:UITableViewRowAnimationNone];
    if(removesectionid!=-1){
        [self.tableview reloadSections:[NSIndexSet indexSetWithIndex:removesectionid]  withRowAnimation:UITableViewRowAnimationNone];
    }
    if([tableview numberOfRowsInSection:sender.tag] ==0 )
    {
         [self updateViewControllerWithIndex:sender.tag];
    }
}

}

我正在使用此代码在单击按钮时显示有关部分重新加载部分数据的数据。长按部分给我一个错误的访问错误。

【问题讨论】:

  • 寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定问题或错误以及重现它所需的最短代码在问题本身。
  • 同意。您是否尝试过明显的事情,例如在未捕获的异常处理程序上设置断点。我怀疑您的问题是您的部分重新加载代码已损坏。
  • 抱歉,点击一下就可以了
  • 使用断点检查你是否handleTap:函数被调用一次、两次或根本没有被调用。

标签: ios objective-c iphone uitableview ios7


【解决方案1】:

不是在内容视图上添加子视图,而是在视图上添加

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-02
    • 1970-01-01
    • 2020-04-27
    • 2014-05-31
    • 1970-01-01
    • 2021-07-18
    • 1970-01-01
    相关资源
    最近更新 更多