ios - 无法自动更新 TableView

标签 ios tableview

我有一个包含许多 View 和一个 TableView 的 View Controller 。

tableview 的单元格是自定义的,因此还有另一个类用于设置单元格。 每个单元格中都有一个按钮。此按钮的图像根据单元格的内容而变化(此内容从数据库中读取)。

基本上,当用户按下按钮时,它会将自己更改为另一个图像,将新状态写入数据库,但 tableview 不会自动更新。

按钮的方法在自定义单元格类中,所以我尝试实例化我的 View Controller (带有 tableview 的那个)并执行一个方法来更新 View 和 tableview 中的一些标签:

ViewControllerWithTable *vc = [[ViewControllerWithTable alloc] init];
[vc updateEverything];

但这行不通。 从同一个“ViewControllerWithTable”(添加一个重新加载按钮)调用的同一个“updateEverything”方法工作得很好。 在 viewWillAppear 方法中添加“[tableView reloadData]”将不起作用,因为所有操作都在同一 View 中完成。

我错过了什么?

编辑:添加一些代码以更加清晰。

这是我用来更新表格 View 的方法。它位于带有嵌入式 tableview 的 ViewController 内部,当由其中一个 View 中的按钮触发时它会工作:

- (void) updateEverything {
    // lots of DB writing and reading, plus label text changing inside all the views
    [tableView reloadData];

}

这是按下按钮的 IBAction,它位于自定义单元格类中:

-(void) btnPresaPressed:(UIButton *)sender {
    AppDelegate *deleg = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    deleg.did = sender.tag;
    NSString *s1 = NSLocalizedString(@"ALERT_TITLE", nil);
    NSString *s2 = NSLocalizedString(@"ALERT_BODY", nil);
    NSString *s3 = NSLocalizedString(@"YES", nil);
    NSString *s4 = NSLocalizedString(@"NO", nil);
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:s1
                                                        message:s2
                                                       delegate:self
                                              cancelButtonTitle:s4
                                              otherButtonTitles:s3, nil];
    [alertView setTag:1];
    [alertView show];

}

此方法显示调用另一个方法的警报 View ,始终在自定义单元格类中:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    AppDelegate *deleg = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    DbOperations *db = [[DbOperations alloc] init];
    NSString *alrtTitle = [alertView buttonTitleAtIndex:buttonIndex];
    NSString *s3 = NSLocalizedString(@"YES", nil);
    NSString *s4 = NSLocalizedString(@"NO", nil);
    switch (alertView.tag) {
        case 1:
            //
            break;
        case 2:
            if ([alrtTitle isEqualToString:s3]) {
                // DB writing and reading
                ViewControllerWithTable *vc = [[ViewControllerWithTable alloc] init];
                [vc updateEverything];
            } else if ([alrtTitle isEqualToString:s4]){
                //

            }
            break;
        case 3:
            if ([alrtTitle isEqualToString:s3]) {
                //
            }
            break;
        default:
            break;
    }

}

在这种情况下,updateEverything 方法不起作用。

最佳答案

添加更多代码后进行编辑:

在以下行中:

        if ([alrtTitle isEqualToString:s3]) {
            // DB writing and reading
            ViewControllerWithTable *vc = [[ViewControllerWithTable alloc] init];
            [vc updateEverything];

您正在完全实例化一个新的 View Controller ,它与显示 TableView 的原始 View Controller 无关。因此,您将更新消息发送到错误的对象。

您需要的是一种机制,让您的单元知道将消息发送到哪个 Controller 是正确的。

一个简单的解决方案是使用 NSNotificationCenter:

  1. View Controller 为某种通知注册自己:

    [[NSNotificationCenter defaultCenter]
    addObserver:self
    selector:@selector(updateEverything:)
    name:kCellSentUpdateMessageNotification
    object:nil];
    
  2. 您的手机发送通知,而不是直接调用消息:

    [[NSNotificationCenter defaultCenter] postNotificationName:kCellSentUpdateMessageNotification object:nil];
    

旧答案:

你应该打电话

  [self.tableView reloadData]

来自您的 updateEverything 方法实现。这将重新加载表数据,有效地更新其行外观。显然,当连续点击按钮时,updateEverything 方法将被调用,这样才能正常工作。

如果这不起作用,请提供更多代码。

关于ios - 无法自动更新 TableView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10893861/

相关文章:

ios - 访问了自定义 UITableViewCell 奇怪的选择行为

ios - 为什么我无法将 UIImageView 作为参数传递?

ios - 如何向具有用户身份验证的网站发出访问其内容的请求?

ios - swift:如何在两个 UITableViewCell 之间翻转单元格

ios - 当我滚动表格 View 时,其单元格会自动更新,如何停止更新单元格?

static - iOS 7 静态表格 View 选择样式错误

ios - NSBundle pathForResource : returns nil

ios - 在 NSMutableArray 之间移动对象时出错

swift - 未调用 UITableView 数据源函数(快速)

ios - 在 TableViewCell 中进行更改