IOS 刷新tableView的section时候出了问题
怪我咯
怪我咯 2017-04-17 14:32:39
[iOS讨论组]

在做一个类似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];
    }
    }
    这个是打断点后的崩溃信息截图,大神们帮忙看看是什么原因导致崩溃的吧~谢谢

怪我咯
怪我咯

走同样的路,发现不同的人生

全部回复(3)
大家讲道理

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

迷茫

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

PHP中文网

我也想知道答案 ,您是怎么解决的

改过来了,我可是不太明白为什么会崩溃

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号