【问题标题】:iOS Obj-C - UITableView data disappears when modal view dismissediOS Obj-C - 当模式视图消失时 UITableView 数据消失
【发布时间】:2017-04-25 02:25:24
【问题描述】:

我在视图控制器中有一个 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)];


}

【问题讨论】:

  • 您的viewWillAppearviewDidAppear 中有任何内容吗?或者任何影响您的模态表中的 tableView 的东西?尝试为您的表格视图添加背景颜色,看看它是否仍然存在或消失
  • @Tj3n 请参阅上面的编辑以了解 viewDidAppear 中的内容。我已经做到了,当用户进入这个视图时,它会自动滚动到 tableview 的底部。我使用的代码会影响这个吗?
  • 为什么要将表格视图的内容偏移设置为这么大的值?这条线的目标是什么?
  • 关闭模态视图后检查数据源(self.messages)的值。

标签: ios objective-c uitableview


【解决方案1】:

问题可能出在你的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

【讨论】:

  • 将行替换为您建议的行会导致崩溃 (EXC_BAD_ACCESS)。
猜你喜欢
  • 1970-01-01
  • 2013-07-28
  • 2021-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-14
  • 2013-04-26
相关资源
最近更新 更多