IOS 刷新tableView的section时候出了问题

在做一个类似QQ好友的tableView,我给表头的view加了个点击事件,用一个BOOL属性判断是否展开二级列表 下面是代码

import "ProvinceAndCityController.h"

@interface ProvinceAndCityController ()
//省份数组
@property(nonatomic,strong)NSMutableArray * provinceArray;
//省份对应的城市列表
@property(nonatomic,strong)NSMutableArray * cityArray;
//是否展开二级列表
@property(nonatomic,assign)BOOL isOpen;
@end

@implementation ProvinceAndCityController

  • (void)viewDidLoad
    {
    [super viewDidLoad];

    //从数据库获得省份信息
    [SQLite getAllCarProvinceAndCallVack:^(id obj) {
    self.provinceArray = obj;
    }];
    [self.tableView setSeparatorInset:UIEdgeInsetsZero];
    self.tableView.bounces = NO;
    }
    //有几个分区

  • (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
    return self.provinceArray.count;
    }
    //分区的行数,如果isOpen为NO就为0行
  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
    return self.isOpen? self.cityArray.count:0 ;
    }
    //设置表头的View
    -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {
    UIView *tempV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, 44)];
    tempV.backgroundColor = [UIColor colorWithRed:(236)/255.0f green:(236)/255.0f blue:(236)/255.0f alpha:1];

    UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(16, 2, 200, 30)];
    label1.backgroundColor = [UIColor clearColor];
    label1.font = [UIFont fontWithName:@"Arial" size:20];
    label1.text = [self.provinceArray[section] objectForKey:@"name"];

    ///给section加一条线。
    CALayer * separatorL = [CALayer layer];
    separatorL.frame = CGRectMake(0.0f, 43.0f, [UIScreen mainScreen].bounds.size.width, 1.0f);
    separatorL.backgroundColor = [UIColor lightGrayColor].CGColor;

    [tempV addSubview:label1];

    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapSection:)];
    tap.numberOfTapsRequired = 1;
    tap.numberOfTouchesRequired = 1;
    //设置tag为点击的section
    tempV.tag = section;
    [tempV addGestureRecognizer:tap];
    [tempV.layer addSublayer:separatorL];
    return tempV;
    }
    //点击表头触发此方法
    -(void)tapSection:(UITapGestureRecognizer * )sender
    {
    self.isOpen = !self.isOpen;
    //点击省份对应的省份ID
    NSInteger ProvinceID = 1;
    //根据点击的省份的ID获取对应的城市信息,数据暂时都用一个
    [SQLite getCarNumByProvinceID:ProvinceID AndCallBack:^(id obj)
    {
    self.cityArray = obj;
    }];
    if (self.cityArray.count>0)
    {
    //刷新对应的表, 就在这里崩溃,改成relodata不会崩
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationFade];
    }
    }
    这个是打断点后的崩溃信息截图,大神们帮忙看看是什么原因导致崩溃的吧~谢谢
    图片描述

阅读 13.3k
3 个回答

上面的问题已经解决,[self.tableView reloadSections:[[NSIndexSet alloc] initWithIndex:self.count] withRowAnimation:UITableViewRowAnimationFade];

新手上路,请多包涵

请问您是怎么解决的,我也碰到了同样地问题?

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题