- 自上而下设置好约束条件(让系统知道cell高度)
- 代码相关:
_tableView.rowHeight = UITableViewAutomaticDimension;
_tableView.estimatedRowHeight = [TYStationListCell cellHeight];
UITableView不进行reload改变Cell的高度
调用UITableview的beginUpdates和endUpdates,这个时候仅仅会去触发heightForRow方法,而不会走cellForRow或其他,所以这种写法最适合这种情况,这种刷新还是自带动画
#import "TYUserBaseInfoCell.h"
#import "TYEdgeInsetsLabel.h"
@interface TYUserBaseInfoCell()<UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UITextView *textView;
@property (weak, nonatomic) IBOutlet TYEdgeInsetsLabel *titleLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *textViewHeightConstraint; //xib中的priority设置为750,否则会有约束警告
@end
@implementation TYUserBaseInfoCell
+ (CGFloat)cellHeight
{
return 30;
}
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
_titleLabel.numberOfLines = 0;
_titleLabel.font = [UIFont systemFontOfSize:15];
_titleLabel.edgeInsets = UIEdgeInsetsMake(4, 0, 4, 0);
_textView.delegate = self;
_textView.layer.borderWidth = 1;
_textView.layer.borderColor = [UIColor grayColor].CGColor;
}
- (void)refreshWithTitle:(NSString *)title content:(NSString *)content
{
_titleLabel.text = title;
_textView.text = content;
_textViewHeightConstraint.constant = _textView.contentSize.height > 100 ? 100 : _textView.contentSize.height;
}
- (void)setEditable:(BOOL)editable
{
_textView.editable = editable;
}
- (void)setContentColor:(UIColor *)contentColor
{
_textView.textColor = contentColor;
}
#pragma mark - UITextViewDelegate
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if ([text isEqualToString:@"\n"]) {
[textView resignFirstResponder];
return NO;
}
else {
return YES;
}
}
- (void)textViewDidChange:(UITextView *)textView
{
if (textView.contentSize.height > 100) {
_textViewHeightConstraint.constant = 100;
}
else {
_textViewHeightConstraint.constant = textView.contentSize.height;
}
if ([_delegate respondsToSelector:@selector(userBaseInfoCell:contentDidChange:)]) {
[_delegate userBaseInfoCell:self contentDidChange:textView.text];
}
}
###controller中实现代理方法
#pragma mark - TYUserBaseInfoCellDelegate
- (void)userBaseInfoCell:(TYUserBaseInfoCell *)cell contentDidChange:(NSString *)content
{
NSInteger index = [_tableView indexPathForCell:cell].row;
//刷新array中内容
[_contentArray replaceObjectAtIndex:index withObject:content];
//只会改变cell的高度,不会reloadcell
[_tableView beginUpdates];
[_tableView endUpdates];
}
网友评论