iOS Obj-C - 当模态视图被解除时,UITableView数据消失

时间:2017-04-25 02:25:24

标签: ios objective-c uitableview

我在视图控制器中有一个uitableview,在我的uitableview下面有一个按钮。点击此按钮可打开模态视图。我使用以下代码在我的模态中创建了一个“关闭”按钮:

modalview.m

- (IBAction)closeButton:(id)sender {

     [self dismissViewControllerAnimated:YES completion:nil];
}

然而,当我点击以关闭我的模态视图时,我的uitableview中的所有数据似乎都消失了(uitableview只是白色)?知道为什么会这样,我该如何解决?以下是我的tableview数据的加载和结构化方式(希望这会有所帮助):

ViewController.m

    -(void)updateMessages {

        self.tableView.dataSource = self;

        NSMutableDictionary *viewParams = [NSMutableDictionary new];
        [viewParams setValue:@"name" forKey:@"view_name"];
        [DIOSView viewGet:viewParams success:^(AFHTTPRequestOperation *operation, id responseObject) {


            self.messages = (NSMutableArray *)responseObject;

            [self.tableView reloadData];


        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"Failure: %@", [error localizedDescription]);
        }];



    }

    - (void)viewDidLoad {
        [super viewDidLoad];

       [self updateMessages];

       static NSString *ChatTableIdentifier = @"ChatTableViewCell";
       static NSString *ChatTableIdentifier2 = @"SwapDetailTableViewCell";


        UINib *nib = [UINib nibWithNibName: ChatTableIdentifier bundle:nil];
        [self.tableView registerNib:nib forCellReuseIdentifier: ChatTableIdentifier];

        UINib *nib2 = [UINib nibWithNibName: ChatTableIdentifier2 bundle:nil];
        [self.tableView registerNib:nib2 forCellReuseIdentifier: ChatTableIdentifier2];


          self.tableView.dataSource = self;

         [self.tableView reloadData];

    }

    - (int)numberOfSectionsInTableView: (UITableView *)tableview

    {
        return 1;

    }


    - (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {

        return [self.messages count];


    }



    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {


        NSDictionary *data = self.messages[indexPath.row];

        id swaptime = data[@"swaptime"];
        if ([swaptime isKindOfClass:[NSString class]]) {
            // There is a valid "swaptime" value
            static NSString *ChatTableIdentifier2 = @"SwapDetailTableViewCell";

            SwapDetailTableViewCell *cell = (SwapDetailTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ChatTableIdentifier2 forIndexPath:indexPath];

            NSString *time = data[@"swaptime"];
            cell.startTime.text = time;

            NSString *timeEnd = data[@"endswaptime"];
            cell.endTime.text = timeEnd;

            NSString *costofSwap = data[@"swapvalue"];
            cell.swapValue.text = costofSwap;

            NSString *fromUsername = data[@"first name"];
            cell.fromUser.text = fromUsername;

            NSString *pet = data[@"pet's name"];
            cell.petsname.text = pet;


            NSString *swapStatus = data[@"swapaccepted"];

            if ([swapStatus isEqual: @"Yes"]) {

            [cell.displayedBar setImage:[UIImage imageNamed:@"greenbar.png"]];

                cell.swapTitle.text = @"Accepted!";}


            else {

                if ([swapStatus isEqual: @""]) {

                    [cell.displayedBar setImage:[UIImage imageNamed:@"orangecellbar.png"]];

                    cell.swapTitle.text = @"You have a Request!";}

                if ([swapStatus isEqual: @"Requested"]) {

                    [cell.displayedBar setImage:[UIImage imageNamed:@"orangecellbar.png"]];

                    cell.swapTitle.text = @"You have a Request!";}

                if ([swapStatus isEqual: @"Cancelled"]) {

                    [cell.displayedBar setImage:[UIImage imageNamed:@"blueecellbar.png"]];

                    cell.swapTitle.text = @"Cancelled!";}

                if ([swapStatus isEqual: @"No"]) {

                    [cell.displayedBar setImage:[UIImage imageNamed:@"redbar.png"]];

                    cell.swapTitle.text = @"Declined!";


            }


          }

            return cell;

        } else {
            static NSString *ChatTableIdentifier = @"ChatTableViewCell";

            ChatTableViewCell *cell = (ChatTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ChatTableIdentifier forIndexPath:indexPath];

            NSString *userName = data[@"first name"];
            cell.sendingUser.text = userName;

            NSString *messageBody = data[@"body"];
            cell.messageDisplayed.text = messageBody;

            NSString *timeReceived = data[@"published at"];
            cell.timeStamp.text = timeReceived;


            NSString *userInfo = [self.userid objectForKey:@"first name"];


            if ([cell.sendingUser.text isEqual: userInfo]) {

                cell.messageDisplayed.textAlignment = NSTextAlignmentLeft;
                cell.sendingUser.textAlignment = NSTextAlignmentLeft;


                [cell.chatBubble setImage:[UIImage imageNamed:@"bubblegrey2.png"]];


            } else {

             cell.messageDisplayed.textAlignment = NSTextAlignmentRight;
             cell.sendingUser.textAlignment = NSTextAlignmentRight;


                [cell.chatBubble setImage:[UIImage imageNamed:@"bubbleorange2.png"]];

            }

            return cell;
        }
    }

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];


    [self.tableView setContentOffset:CGPointMake(0, CGFLOAT_MAX)];


}

1 个答案:

答案 0 :(得分:0)

问题可能出在viewDidAppear,请尝试替换:

[self.tableView setContentOffset:CGPointMake(0, CGFLOAT_MAX)];

使用:

[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:[self.messages count]-1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];

或正确计算高度,可能是:

self.tableView.contentView.height