【问题标题】:Reload a tableView inside viewController在 viewController 中重新加载 tableView
【发布时间】:2014-03-26 18:12:10
【问题描述】:

我在我的故事板上设置了一个视图控制器,并且我在这个视图控制器中插入了一个 tableView。我想在 viewDidLoad 上执行 [self.tableView reloadData];,但它说找不到属性 tableView。

下面是 viewcontroller.m 文件中我的 tableView 的代码:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section     {
    return @"Shared with";
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return activeUsers.count;
}

- (sharedUsersTable *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"sharedUser";

    sharedUsersTable *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[sharedUsersTable alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    NSDictionary *key = [activeUsers objectAtIndex:indexPath.row];
    NSString *name = [key objectForKey:@"shared_user_name"];
    NSString *email = [key objectForKey:@"email"];

    cell.textLabel.text = [NSString stringWithFormat:@"%@", name];
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@", email];

    return cell;
}

我是否缺少一些特殊命令来在不是 TableViewCONtroller 的视图控制器中调用 tableView?

【问题讨论】:

  • 你设置了delegate和dataSource吗?
  • 恐怕不行。我究竟在哪里定义这些?

标签: ios objective-c uitableview


【解决方案1】:

您需要在 tableView 中创建一个 IBOutlet 并为此调用 reloadData

【讨论】:

  • 谢谢,应该可以。但是,视图没有加载数据。如果我的 tableview 被称为 sharedView 我应该如何修改上面的代码以使其正确呈现?我现在假设,我的代码没有定义正确的 tableView。
  • 您必须分配委托和数据源。使用tableView.datasource = self;tableView.delegate = self;
  • 我在哪里定义这些?
【解决方案2】:

这取决于您如何声明您的UITableView

如果它是一个属性 (@property (nonatomic, strong) UITableView *myTableView),它将是 [self.myTableView reloadData]。如果您将它声明为一个实例变量 (UITableView *_myTableView),它将是 [_myTableView reloadData]

【讨论】:

    【解决方案3】:

    您必须将 tableview 数据源和委托定义为 self.. 像 table.datasource=self, 和table.delegate=self.....您可以在viewdidload 中执行此操作....但是如果您在某处下载了数据,那么您可以在您下载的那个地方执行此操作并在那里重新加载表格。请在下载数据后将委托和数据源分配给表格并重新加载表格视图....并且请在重新加载时创建(@property (nonatomic, strong) UITableView *table)之类的属性并使用self...

    【讨论】:

      猜你喜欢
      • 2017-11-14
      • 2014-05-09
      • 2020-03-25
      • 2021-05-28
      • 1970-01-01
      • 1970-01-01
      • 2016-11-23
      • 2015-02-20
      • 2015-08-12
      相关资源
      最近更新 更多