gpt4 book ai didi

ios - Obj-C - TableView 在数据重新加载后滚动到底部?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:30:38 26 4
gpt4 key购买 nike

我试图在加载新数据后让我的表格 View 滚动到底部。例如,我有一个聊天窗口,一旦我的“发送回复”按钮被按下,它就会加载新消息,我总是希望它在发布数据时滚动到最新的消息(在底部)。出于某种原因,我的 tableView 在 self.tableview 重新加载后不会滚动到底部?

我假设这是因为我的方法中有 rowNumber-1,但是当我尝试将其更改为 -0 时,出现错误:

Terminating app due to uncaught exception 'NSRangeException', reason: '-[UITableView _contentOffsetForScrollingToRowAtIndexPath:atScrollPosition:usingPresentationValues:]: row (16) beyond bounds (16) for section (0).'

ViewController.m

- (void)scrollTableToBottom {
NSInteger rowNumber = [self. tableView numberOfRowsInSection:0];
if (rowNumber > 0) {
[self. tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:rowNumber-1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}
}

- (IBAction)sendReply:(id)sender {


if ([self.messageData count] > 0) {

self.sendButton.hidden = YES;

self.activityIndicator.hidden = NO;
[self.activityIndicator startAnimating];


NSMutableDictionary *nodeData = [NSMutableDictionary new];

[nodeData setObject:@"messages" forKey:@"type"];


NSDictionary *messageValues = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:self.replyField.text, nil] forKeys:[NSArray arrayWithObjects:@"value", nil]];
NSDictionary *finalMessage = [NSDictionary dictionaryWithObject:[NSArray arrayWithObject:messageValues] forKey:@"und"];

[nodeData setObject:finalMessage forKey:@"body"];

NSString *otherUID = [self.messageData objectForKey:@"uid"];



NSString *userValue = otherUID;


NSDictionary *targetMessage = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:userValue, nil] forKeys:[NSArray arrayWithObjects:@"value", nil]];
NSDictionary *finalUser = [NSDictionary dictionaryWithObject:[NSArray arrayWithObject:targetMessage] forKey:@"und"];

[nodeData setObject:finalUser forKey:@"field_targetuser"];

[nodeData setValue: @"Re:" forKey:@"title"];

NSDictionary *userDictInfo = (NSDictionary*) [NSKeyedUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] objectForKey:@"diosSession"]];


DIOSSession *session = [DIOSSession sharedSession];
[session setUser:userDictInfo];


[session user];


NSString *uid = [session user][@"user"][@"uid"];


[DIOSNode nodeSave:nodeData success:^(AFHTTPRequestOperation *operation, id responseObject) {


self.replyField.text = @"";
[self.tableView reloadData];
[self scrollTableToBottom];
self.sendButton.hidden = NO;
[self.activityIndicator stopAnimating];
self.activityIndicator.hidden = YES;


} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Node did not save!");
}];



} else if ([self.messageDataFriends count] > 0) {

self.sendButton.hidden = YES;

self.activityIndicator.hidden = NO;
[self.activityIndicator startAnimating];

NSMutableDictionary *nodeData = [NSMutableDictionary new];

[nodeData setObject:@"messages" forKey:@"type"];


NSDictionary *messageValues = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:self.replyField.text, nil] forKeys:[NSArray arrayWithObjects:@"value", nil]];
NSDictionary *finalMessage = [NSDictionary dictionaryWithObject:[NSArray arrayWithObject:messageValues] forKey:@"und"];

[nodeData setObject:finalMessage forKey:@"body"];


NSString *otherUID = [self.messageDataFriends objectForKey:@"uid2"];



NSString *userValue = otherUID;

NSDictionary *targetMessage = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:userValue, nil] forKeys:[NSArray arrayWithObjects:@"value", nil]];
NSDictionary *finalUser = [NSDictionary dictionaryWithObject:[NSArray arrayWithObject:targetMessage] forKey:@"und"];

[nodeData setObject:finalUser forKey:@"field_targetuser"];


[nodeData setValue: @"Re:" forKey:@"title"];


NSString *uid = [[[DIOSSession sharedSession] user] objectForKey:@"uid"];



[DIOSNode nodeSave:nodeData success:^(AFHTTPRequestOperation *operation, id responseObject) {

dispatch_async(dispatch_get_main_queue(), ^{

self.replyField.text = @"";


[self.tableView reloadData];
[self scrollTableToBottom];
self.sendButton.hidden = NO;
[self.activityIndicator stopAnimating];
self.activityIndicator.hidden = YES;


});




} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Node did not save!");
}];


}


}

最佳答案

  1. 添加到第一个 block

    dispatch_async(dispatch_get_main_queue(), ^{
    self.replyField.text = @"";
    [self.tableView reloadData];
    [self scrollTableToBottom];
    self.sendButton.hidden = NO;
    [self.activityIndicator stopAnimating];
    self.activityIndicator.hidden = YES;
    });
  2. 还在 NSInteger 之后添加 NSLog(@"rowNumber = %lu",rowNumber);rowNumber = [self.tableView numberOfRowsInSection:0]; 在滚动表底部方法。在 [self.tableView reloadData]; 之前如果它们相等,则意味着数据尚未重新加载。

关于ios - Obj-C - TableView 在数据重新加载后滚动到底部?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47726005/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com