关于点击TableviewCell的子内容收放问题,拿到它的第一个思路就是,
方法一:
运用UITableview本身的代理来处理相应的展开收起:
1.代理:- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
2. 需要声明一个全局BOOL变量isOpen,记录当前cell的状态(展开/收起),声明一个NSInterge类型selectedIndexRow,记录选择的cell的row,切记初始化相应的属性。
(ps:在网上看到很多文章都是这样,但是真的用的时候,发现,我们需要另外声明一个NSIndexPath类型的selectedIndex,或者用到时候自己运用记录的row生成也可,也许确实是我自己多此一举)
3.首先,我们需要理清自己需求的逻辑关系,什么时候展开/收起,展开收起时它的高度,个数等等有什么变化------->来进行代理,数据源方法的书写
下面也是展示tableview时的调用顺序
1>://返回cell个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
2>://返回每行的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
3>://请求数据元代理为tableView插入需要的cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
4>://监听点击的cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
代码:
#pragma mark --------- UITableViewDelegate && UITableViewDataSource -----
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.scoreDataArray.count;//根据自己的具体需要返回
}
//计算高度---根据需求,动态计算内容的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//indexPath.row == selectedIndex.row &&
NSLog(@"heightForRow %ld",(long)selectedIndex.row);
if (indexPath.row == selectedIndex.row && selectedIndex != nil)
{
if (isOpen == YES)
{
//内部内容直接忽略即可
YunGangScoreData *data = [[YunGangScoreData alloc] init];
//data = self.scoreDataArray[indexPath.row];
data = self.scoreDataArray[selectedIndex.row];
if (data.detailTypeArray.count>0)
{
self.cellHeight = 60+data.detailTypeArray.count*40;
return self.cellHeight;
}
return 60;
}
else
{
return 60;
}
}
return 60;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//indexPath.row == selectedIndex.row &&
NSLog(@"cellForRow %ld",(long)selectedIndex.row);
if (indexPath.row == selectedIndex.row && selectedIndex != nil)
{//内部内容直接忽略即可
//如果是展开
if (isOpen == YES)
{
YunGangScoreTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YunGangScoreTableViewC