【问题标题】:UITableviewcontroller reload data issueUITableviewcontroller 重新加载数据问题
【发布时间】:2014-02-19 07:25:54
【问题描述】:

请原谅我对 iPhone 编程的无知,因为我是它的新手。 我正在制作一个应用程序,其中初始化视图控制器是用于显示数据的 UITable 视图控制器和用于输入数据的另一个视图控制器。

我的问题是当我输入新数据时,它不会出现在 UItable 控制器中,我尝试在 viewDidLoad 中使用 [self.tableview reloadData] 重新加载数据,但数据不会重新加载,因为我使用的是自定义委托 我尝试将[self.tableview reloadData] 作为我的自定义委托的最后一行,但它也不起作用,我尝试使用viewDidAppear,但这也不起作用。

代码如下:

MainViewController.m (TableView)

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSLog(@"Initializing conference array");

    conferenceData = [[NSMutableArray alloc]init];

    SavingData *loadData = [[SavingData alloc]init];
    NSLog(@"Loading the file then fill it in an array");
    conferenceData = [loadData LoadDataArray];

    NSLog(@"Done Initializing conference array");

    [self.tableView reloadData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    return 1;
}

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

    return [conferenceData count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];

    NSLog(@"writing data in cell number %i",indexPath.row);
    cell.textLabel.text = [[conferenceData objectAtIndex:indexPath.row]name];

    return cell;
}

// Implementing the adding delegate
-(void) setInformation:(Adding *)controller withObject:(Conference *)info
{
    Conference *confer = info; //Temprory Conference object to store data
    SavingData *saveToFile = [[SavingData alloc]init];

    if ([saveToFile SavingDataWithObject:confer]) //Calling SavingDataWithObject method and check if the save operation return true or false
    {
        NSLog(@"Done Saving !!");
    }
    [self.navigationController popToRootViewControllerAnimated:YES]; // after saving return to the main window
    NSLog(@"Should be popped right now");
}

非常感谢您

【问题讨论】:

    标签: objective-c uitableview ios7 xcode5 reloaddata


    【解决方案1】:

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];

    此处的单元格可以为 nil,因此您实际上可能并未返回单元格。

    尝试使用

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    这个函数永远不会返回 nil。

    但首先你必须注册 @Cell 是什么。

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
        ...
     }
    

    或者,您可以检查 cell 是否为 nil,如果不是则创建它,但我认为这种方法更简洁。

    最后一个想法,仔细检查当您调用 reloadData 时,您的数组实际上已填充,您可以在 cellForRowAtIndexPath 中设置一个断点,以确保它被调用了适当的次数。

    【讨论】:

      【解决方案2】:

      我发现了问题

      显然,当我在-viewWillAppear 方法中编写代码时,它工作正常,表重新加载数据。

      非常感谢你帮助我

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-21
        • 1970-01-01
        • 1970-01-01
        • 2019-09-19
        • 2011-03-04
        • 2011-01-01
        相关资源
        最近更新 更多