在viewController.h文件中
@property (nonatomic) NSInteger dataSource;
@property (nonatomic) UITableView *chatTableView;
在viewController.m文件中
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(tableViewScrollToBottom) name:UIKeyboardDidShowNotification object:nil];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter]removeObserver:self];
}
- (void)tableViewScrollToBottom {
if (self.dataSource.count==0)
return;
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.dataSource.count-1 inSection:0];
[self.chatTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}
在 viewDidAppear: 方法中调用 tableViewScrollToBottom 方法,或是在数据加载时调用 tableViewScrollToBottom 方法都可以。