【问题标题】:Unable to reloaddata(UITableView)无法重新加载数据(UITableView)
【发布时间】:2013-03-28 06:31:22
【问题描述】:

我正在 UIView 控制器中创建 UITableView

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 470, self.view.frame.size.height) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
    NSLog(@"tableView is '%@'",self.tableView);
}

NSLog 结果:"tableView is ''"

当itemArray被其他控制器修改时,会调用refreshTableView方法

**SideBarViewController *sideBarViewController = [[SideBarViewController alloc]init];
[sideBarViewController refreshTableView];**

-(void)refreshTableView {
    [self createItemArray];
    NSLog(@"reload itemArray->%@",self.itemArray);
    NSLog(@"tableView is '%@'",self.tableView);
    [self.tableView reloadData];
}

不知何故,NSLog 结果变成:“tableView is '(null)'”。并且 tableView 不会被重新加载。

为什么?请帮忙。谢谢。

更新:

itemArray 的 NSLog 没问题:

改变之前 重新加载 itemArray->( “测试.md” )

更改后 重新加载 itemArray->( “测试.md”, “test2.md” )

更新2:

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.itemArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    cell.textLabel.text = [self.itemArray objectAtIndex:indexPath.row];
    return cell;
}

更新 3:

.h
@interface SideBarViewController : UIViewController<IIViewDeckControllerDelegate,UITableViewDataSource,UITableViewDelegate>

.m
@interface SideBarViewController ()
@property (nonatomic,strong) UITableView *tableView;
@end

@implementation SideBarViewController
@synthesize tableView = _tableView;

更新 4:

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath 

方法工作正常,删除行后重新加载表视图。

【问题讨论】:

  • 在重新加载表之前,它的工作正常吗??
  • 是的,工作正常,只是无法重新加载tableview
  • 您提供的代码看起来很好 - 错误必须在您的代码中的其他地方。
  • 尝试用这一行重新加载表:[[self tableView] reloadData];
  • 在重新加载表格时检查表格视图中的数组。

标签: objective-c uitableview reloaddata


【解决方案1】:

分享更多代码,以便我们找到确切的问题。

你能检查一下你的 tableView 的实际名称吗?分享您在.h文件中声明的表的一些代码。

检查数组,数据在重新加载或更新之前是否可用,并且表格视图方法工作正常。

检查您是否在 .h 文件中定义了 tableView 委托?并在你的类中正确实现 tableView 的委托方法..

@interface MyViewController:UIViewController

还有一件事,检查您是否正确实现了数据源和 delagate 方法。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

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

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

如果你正确地完成了这件事,那么在加载之前检查你的数组是否包含数据。或许这会对你有所帮助。

【讨论】:

    【解决方案2】:

    您可以尝试使用通知

    1. 在 Sidebarvc 中的 viewDidLoad 中添加观察者

      [[NSNotificationCenter defaultCenter] addObserver:self 选择器:@选择器(刷新表视图:) 名称:@“dataChanged” 对象:无];

    2. 创建一个刷新方法

      -(void)refreshTableView:(NSNotification *)notif{ }

    3. 在其他VC上,等待通知

      [[NSNotificationCenter defaultCenter] postNotificationName:@"dataChanged" 对象:无];

    4.别忘了清理它

    add [[NSNotificationCenter defaultCenter] removeObserver:self]; to ViewDidUnload
    

    应该可以。

    【讨论】:

      【解决方案3】:
        //
          //  ViewController.h
          //  tableData
          //
          //  Created by Nirav Jain on 3/28/13.
          //  Copyright (c) 2013 SI. All rights reserved.
          //
      
          #import <UIKit/UIKit.h>
      
          @interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
          {
              int rows;
          }
          @end
      
      
      //  tableData
      //
      //  Created by Nirav Jain on 3/28/13.
      //  Copyright (c) 2013 SI. All rights reserved.
      //
      
      #import "ViewController.h"
      
      //@implementation SideBarViewController
      
      
      @interface ViewController ()
      @property (nonatomic,strong) UITableView *tableView;
      @end
      
      @implementation ViewController
      
      @synthesize tableView = _tableView;
      
      - (void)viewDidLoad
      {
          [super viewDidLoad];
          self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 470, self.view.frame.size.height) style:UITableViewStylePlain];
          self.tableView.dataSource = self;
          self.tableView.delegate = self;
          [self.view addSubview:self.tableView];
          NSLog(@"tableView is '%@'",self.tableView);
          rows = 5;
      
      
          self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonItemStyleDone target:self action:@selector(refreshTableView)];
      }
      
      
      -(void)refreshTableView {
      
          rows = 12;
          NSLog(@"tableView is '%@'",self.tableView);
          [self.tableView reloadData];
      }
      #pragma mark - Table view data source
      - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
          return 1;
      }
      
      - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
      {
          return rows;
      }
      
      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
      {
          static NSString *CellIdentifier = @"Cell";
          UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
          if (cell == nil) {
              cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
          }
      
          cell.textLabel.text =  [ NSString  stringWithFormat:    @"%d", indexPath.row];
          return cell;
      }
      
      - (void)didReceiveMemoryWarning
      {
          [super didReceiveMemoryWarning];
          // Dispose of any resources that can be recreated.
      }
      
      @end
      

      【讨论】:

      • 如果我在 ViewController 中调用 refreshTableView 方法,它会被找到,但是我从其他控制器调用它,tableView 是 '(null)',如我的示例所示。 SideBarViewController *sideBarViewController = [[SideBarViewController alloc]init]; [sideBarViewController refreshTableView];
      • 如果你愿意,我可以通过电子邮件将我的项目发给你吗?
      • 发给我 niravjain02@gmail.com
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-19
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      • 2015-04-14
      • 1970-01-01
      相关资源
      最近更新 更多