Ios 在ScrollTabletoBottom中发布
我正在用ios开发一个聊天应用程序,我有自定义的tableview和UIView,底部有GrowingTextview和按钮。我想在按下Send按钮时将Tableview移到底部。但它无法正确地滚动到底部 我的代码Ios 在ScrollTabletoBottom中发布,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我正在用ios开发一个聊天应用程序,我有自定义的tableview和UIView,底部有GrowingTextview和按钮。我想在按下Send按钮时将Tableview移到底部。但它无法正确地滚动到底部 我的代码 -(void) textViewDidChange:(UITextView *)textView { CGFloat maxHeight = [JSMessageInputView maxHeight]; CGSize size =
-(void) textViewDidChange:(UITextView *)textView
{
CGFloat maxHeight = [JSMessageInputView maxHeight];
CGSize size = [textView sizeThatFits:CGSizeMake(textView.frame.size.width, maxHeight)];
CGFloat textViewContentHeight = size.height;
// End of textView.contentSize replacement code
BOOL isShrinking = textViewContentHeight < self.previousTextViewContentHeight;
CGFloat changeInHeight = textViewContentHeight - self.previousTextViewContentHeight;
if(!isShrinking && self.previousTextViewContentHeight == maxHeight) {
changeInHeight = 0;
}
else {
changeInHeight = MIN(changeInHeight, maxHeight - self.previousTextViewContentHeight);
}
if(changeInHeight != 0.0f) {
// if(!isShrinking)
// [self.inputToolBarView adjustTextViewHeightBy:changeInHeight];
[UIView animateWithDuration:0.25f
animations:^{
UIEdgeInsets insets = UIEdgeInsetsMake(0.0f,
0.0f,
self.ChatTableView.contentInset.bottom + changeInHeight,
0.0f);
self.ChatTableView.contentInset = insets;
self.ChatTableView.scrollIndicatorInsets = insets;
[self scrollToBottomAnimated:NO];
if(isShrinking) {
// if shrinking the view, animate text view frame BEFORE input view frame
[self.inputToolBarView adjustTextViewHeightBy:changeInHeight];
}
CGRect inputViewFrame = self.inputToolBarView.frame;
self.inputToolBarView.frame = CGRectMake(0.0f,
inputViewFrame.origin.y - changeInHeight,
inputViewFrame.size.width,
inputViewFrame.size.height + changeInHeight);
if(!isShrinking) {
[self.inputToolBarView adjustTextViewHeightBy:changeInHeight];
}
}
completion:^(BOOL finished) {
}];
self.previousTextViewContentHeight = MIN(textViewContentHeight, maxHeight);
}
self.inputToolBarView.sendButton.enabled = ([textView.text trimWhitespace].length > 0);
}
-(void)textViewDidBeginEditing:(UITextView *)textView
{
if(!self.previousTextViewContentHeight)
self.previousTextViewContentHeight = textView.contentSize.height;
[self scrollToBottomAnimated:YES];
}
- (void)scrollToBottomAnimated:(BOOL)animated
{
NSInteger rows = [self.ChatTableView
numberOfRowsInSection:0];
if(rows > 0) {
[self.ChatTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:rows - 1 inSection:0]
atScrollPosition:UITableViewScrollPositionBottom
animated:animated];
}
}
- (void)keyboardWillShowHide:(NSNotification *)notification
{
CGRect keyboardRect = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
UIViewAnimationCurve curve = [[notification.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue];
double duration = [[notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
[UIView animateWithDuration:duration
delay:0.0f
options:[UIView animationOptionsForCurve:curve]
animations:^{
CGFloat keyboardY = [self.view convertRect:keyboardRect fromView:nil].origin.y;
CGRect inputViewFrame = self.inputToolBarView.frame;
CGFloat inputViewFrameY = keyboardY - inputViewFrame.size.height;
// for ipad modal form presentations
CGFloat messageViewFrameBottom = self.view.frame.size.height - INPUT_HEIGHT;
if(inputViewFrameY > messageViewFrameBottom)
inputViewFrameY = messageViewFrameBottom;
self.inputToolBarView.frame = CGRectMake(inputViewFrame.origin.x,
inputViewFrameY,
inputViewFrame.size.width,
inputViewFrame.size.height);
UIEdgeInsets insets = self.originalTableViewContentInset;
insets.bottom = self.view.frame.size.height - self.inputToolBarView.frame.origin.y - inputViewFrame.size.height;
self.ChatTableView.contentInset = insets;
self.ChatTableView.scrollIndicatorInsets = insets;
}
completion:^(BOOL finished) {
}];
}
-(void)sendPressed:(id)sender {
NSString*TextString;
TextString=_inputToolBarView.textView.text;
NSString *rawString = [self.inputToolBarView.textView text];
NSCharacterSet *whitespace = [NSCharacterSet whitespaceAndNewlineCharacterSet];
NSString *trimmed = [rawString stringByTrimmingCharactersInSet:whitespace];
_inputToolBarView.textView.text = trimmed;
if ([TextString isEqualToString:@""]) {
NSLog(@"Empty");
}
else
{
if([sharedClass.unmatchcheckStr isEqualToString:@"unMatched"])
{
UIAlertController * alert= [UIAlertController
alertControllerWithTitle:[NSString stringWithFormat:@"%@ Unmatched you",_userName]
message:[NSString stringWithFormat:@"%@",UNMATCH_CHAT_ALERT]
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* okButton = [UIAlertAction
actionWithTitle:@"Ok"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
sharedClass.unmatchcheckStr=@"";
[[ChatConnectClass sharedManager]deleteUserRecord:[NSString stringWithFormat:@"%@",_toUserID]];
[self performSegueWithIdentifier:@"match" sender:self];
}];
[alert addAction:okButton];
[self presentViewController:alert animated:YES completion:nil];
}
else
{
NSDate *currentDateNSDate = [NSDate date];
NSDateFormatter *dateFormatterCurrent = [[NSDateFormatter alloc] init];
[dateFormatterCurrent setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *time = [dateFormatterCurrent stringFromDate:currentDateNSDate];
NSDate *currentDate = [dateFormatterCurrent dateFromString:time];
int random = arc4random() % 10000;
NSString *messageId = [NSString stringWithFormat:@"%@%lu%d",[[MontageUserDefaults getBasicInfo] valueForKey:kMemberId],(unsigned long)([[NSDate date] timeIntervalSince1970]*10.0),random];
NSLog(@"MSG id Sent : %@",messageId);
[[ChatConnectClass sharedManager]insertNewMessages:[NSString stringWithFormat:@"%@",_toUserID] message:_inputToolBarView.textView.text messageId:messageId date:currentDate isFrom:NO msgStatus:@"Sending"];
[[ChatConnectClass sharedManager] sendMessage:_inputToolBarView.textView.text messageId:messageId To:self.toUserID];
array_recieveMessage.array = [[ChatConnectClass sharedManager] fetchMessagesUsingUserid:[NSString stringWithFormat:@"%@",_toUserID]];
[self.ChatTableView reloadData];
[self scrollToBottomAnimated:YES];
_inputToolBarView.textView.text=@"";
}
}
}
-(无效)textViewDidChange:(UITextView*)textView
{
CGFloat maxHeight=[JSMessageInputView maxHeight];
CGSize size=[textView sizeThatFits:CGSizeMake(textView.frame.size.width,maxHeight)];
CGFloat textViewContentHeight=size.height;
//textView.contentSize替换代码结束
BOOL isShrinking=textViewContentHeight0);
}
-(无效)textViewDidBeginEditing:(UITextView*)textView
{
如果(!self.previousTextViewContentHeight)
self.previousTextViewContentHeight=textView.contentSize.height;
[自滚动至底部动画:是];
}
-(void)滚动到底部动画化:(BOOL)动画化
{
NSInteger行=[self.ChatTableView
节中的行数:0];
如果(行数>0){
[self.ChatTableView ScrollToRowatineXpath:[NSIndexPath indexPathForRow:rows-1第1节:0]
atScrollPosition:UITableViewScrollPositionBottom
动画:动画];
}
}
-(无效)键盘将显示隐藏:(NSNotification*)通知
{
CGRect keyboardRect=[[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue];
UIViewAnimationCurve=[[notification.userInfo objectForKey:UIKeyboardAnimationCurveUserInfo]integerValue];
double duration=[[notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]doubleValue];
[UIView animateWithDuration:持续时间
延迟:0.0f
选项:[UIView AnimationOptions for curve:curve]
动画:^{
CGFloat keyboardY=[self.view convertRect:keyboardRect fromView:nil].origin.y;
CGRect inputViewFrame=self.inputToolBarView.frame;
CGFloat inputViewFrameY=键盘Y-inputViewFrame.size.height;
//用于ipad模态形式演示
CGFloat messageViewFrameBottom=self.view.frame.size.height-输入\单位高度;
如果(inputViewFrameY>messageViewFrameBottom)
inputViewFrameY=messageViewFrameBottom;
self.inputToolBarView.frame=CGRectMake(inputViewFrame.origin.x,
inputViewFrameY,
inputViewFrame.size.width,
inputViewFrame.size.height);
UIEdgeInsets insets=self.originalTableViewContentInset;
insets.bottom=self.view.frame.size.height-self.inputToolBarView.frame.origin.y-inputViewFrame.size.height;
self.ChatTableView.contentInset=插入;
self.ChatTableView.ScrollIndicationSets=插入;
}
完成:^(布尔完成){
}];
}
-