iOS中Cell的展开和收起
2017-05-22 本文已影响1553人
MR小锦
首先,先上图,让大家看看效果
![](https://img.haomeiwen.com/i3898441/35ea6f51aa2ec95c.png)
相信大家对于TableViewd数据的设置都熟悉,这方面就不多说的,重点的还是来看:
1.如何实现cell的展开和收起的效果
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.tableView deselectRowAtIndexPath:indexPath animated:NO];
currentRow = indexPath.row;
NSDictionary *sectionDic = self.dataSource[indexPath.section];
NSArray *cellArray = sectionDic[@"sub"];
//cell当前的数据
NSDictionary *cellData = cellArray[indexPath.row];
NSString *key = [NSString stringWithFormat:@"%@", cellData[@"chapterID"]];
CellModel *chapterModel = [self.cellOpen valueForKey:key];
chapterModel.isShow = !chapterModel.isShow;
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
当用户点击到某一个cell时候,需要判断cell是否是展开状态,如果张开或者收起就调用
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
让cell的section能够重新加载刷新;
2.如何增加cell的某一个Section的row
2.1设置好对用是否展开row的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *sectionDic = self.dataSource[indexPath.section];
NSArray *cellArray = sectionDic[@"sub"];
//cell当前的数据
NSDictionary *cellData = cellArray[indexPath.row];
NSString *key = [NSString stringWithFormat:@"%@", cellData[@"chapterID"]];
CellModel *model = [self.cellOpen valueForKey:key];
if (model.isShow) {
return (model.pois.count+1)*60;
} else {
return 60;
}
}
2.2 返回对应的section的row
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
BOOL sectionStates = [self.sectionOpen[section] boolValue];
if(sectionStates)
{
//数据决定显示多少行cell
NSDictionary *sectionDict = self.dataSource[section];
//section决定cell的数据
NSArray *cellArray = sectionDict[@"sub"];
return cellArray.count;
}
else
{
//section是收起的时候
return 0;
}
}