效果图 :
cell的删除与插入.gif数据来源 :
plist1.png plist2.png主要代码 解析:
1.点击每组的代理 -- 控制器中实现
pragma mark - ExpandHeaderFooterViewDelegate
- (void)didSelectedHeaderFooterView: (ExpandHeaderFooterView *)headerFooterView {
// 1.关闭展开的组
if (self.isOpen && self.lastSelectedSection != headerFooterView.tag) {
self.isOpen = NO;
// 先收起之前组的箭头
ExpandHeaderFooterView *view = (ExpandHeaderFooterView *)[self.tableView headerViewForSection:self.lastSelectedSection];
[view changeArrowWithIsOpen:NO];
// 删除所展开的cell
NSIndexPath *deleteIndexPath = [NSIndexPath indexPathForRow:0 inSection:self.lastSelectedSection];
[self.tableView deleteRowsAtIndexPaths:@[deleteIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
// 2.点击同一组处理
if (self.isOpen && self.lastSelectedSection == headerFooterView.tag) {
self.isOpen = NO;
// 删除所展开的cell
NSIndexPath *deleteIndexPath = [NSIndexPath indexPathForRow:0 inSection:self.lastSelectedSection];
[self.tableView deleteRowsAtIndexPaths:@[deleteIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
return ;
}
// 3.展开所点击的组(非同一组)
self.isOpen = YES;
self.lastSelectedSection = headerFooterView.tag;
NSIndexPath *insertIndexPath = [NSIndexPath indexPathForRow:0 inSection:headerFooterView.tag];
[self.tableView insertRowsAtIndexPaths:@[insertIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
2.模型中的代码 :
---.h文件
@property (nonatomic, copy) NSString *title;
@property (nonatomic, strong) NSArray *texts;
/// 自定义属性
/// 存放每个组中文本的计算高度, 用于在cell中创建label时设置高度
@property (nonatomic, strong, readonly) NSArray *everyTextHeightArray;
/// 每个cell的最终高度
@property (nonatomic, assign) CGFloat cellHeight;
@end
---.m文件
- (NSArray *)everyTextHeightArray {
NSMutableArray *tmpMarray = [NSMutableArray array];
_cellHeight = 0;
for (int i = 0; i<_texts.count; i++) {
NSString *text = _texts[i];
CGFloat textHeight = [text boundingRectWithSize:CGSizeMake(APP_SCREEN_WIDTH - 40, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:14]} context:nil].size.height + 15; // 加15: 每个label之间的距离
_cellHeight += textHeight;
[tmpMarray addObject:@(textHeight)];
}
return tmpMarray;
}
网友评论