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

时间:2017-12-09 06:30:57

标签: ios objective-c uitableview

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

我认为这是因为我的方法中的 rowNumber-1 ,但当我尝试将其更改为-0时,我收到错误:

  

由于未捕获的异常终止应用' NSRangeException',原因:   ' - [UITableView的   _contentOffsetForScrollingToRowAtIndexPath:atScrollPosition:usingPresentationValues:]:   对于(0)部分,超出边界(16)的行(16)。'

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 个答案:

答案 0 :(得分:1)

  1. 添加到第一个块

    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. NSLog(@"rowNumber = %lu",rowNumber);之后添加NSInteger rowNumber = [self.tableView numberOfRowsInSection:0]; scrollTableToBottom方法。在[self.tableView reloadData];之前 如果它们相等,则表示数据尚未重新加载。