许多app的界面由于cell太长,需要展开与收起
@protocol ZJICommentsViewDelegate <NSObject>
- (void)clickFoldLabel:(UIButton *)btn;
- (void)clickShortFoldLabel:(UIButton *)btn;
@end
- (void)clickNewsOrNo:(UIButton *)btn{
if(self.commentsViewDelegate && [self.commentsViewDelegate respondsToSelector:@selector(clickFoldLabel:)]){
[self.commentsViewDelegate clickFoldLabel:btn];
}
}
controller里cell的点击事件
注意:最好不要用button.selected控制开关,可以设置一个属性控制button是什么状态 比如下面的 self.isLongSelected
- (void)clickFoldLabel:(UIButton *)btn{
ZJILongCommitTableViewCell *cell = (ZJILongCommitTableViewCell *)[[btn superview] superview];//通过button获取父视图
NSIndexPath *indexPath = [self.customView.tableView indexPathForCell:cell];
ReplytoModel *reply = [self.customView.longCommentModel.comments[indexPath.row] replyto];
if(self.isLongSelected){
self.isLongSelected = NO;
reply.isOpening = NO;
}else{
self.isLongSelected = YES;
reply.isOpening = YES;
}
[_customView.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}
在计算高度和赋值那里用model的isOpening属性
⚠️注意:在自定义cell 里layoutSubViews,先不要给self.replyToLabel.numberOfLines 赋值,这个赋值之后后面无法改变这个numberOflines属性(虽然可以改变值,但是界面无法显示改变)
if(reply.isOpening || reply.isShortOpening){
self.replyToLabel.numberOfLines = 0;
[self.openButton setTitle:@"收起" forState:UIControlStateNormal];
}else{
self.replyToLabel.numberOfLines = 3;
[self.openButton setTitle:@"展开" forState:UIControlStateNormal];
}
计算高度用数组缓存一下计算的高度,不用每次都算
if(replyModel && replyModel.isOpening){
return [_cellHeightOnArray[indexPath.row] floatValue];//展开的高度
}
return [_cellHeightArray[indexPath.row] floatValue];//收起的高度