ios tableView reloadData 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19302270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:16:31  来源:igfitidea点击:

tableView reloadData not working

iphoneiosobjective-cuitableview

提问by iCode

When I call [tableView reloadData]the function - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPathis not fired? Why?

当我调用[tableView reloadData]该函数时- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath没有被触发?为什么?

Here's my TableView.m

这是我的TableView.m

@implementation TableView
@synthesize managedObjectContext;
@synthesize controller = _controller;
@synthesize tableView = _tableView;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

    }
    return self;
}
-(id)init
{
    self = [super init];
    if (self) {
        NSLog(@"%s",__PRETTY_FUNCTION__);
        self.del = [[UIApplication sharedApplication] delegate];
        self.managedObjectContext = self.del.managedObjectContext;

        [self performControllerFetch];
    }
    return self;
}


#pragma mark - Table view data source

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

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

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

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

    ...
}

-(void)reloadTableView
{
    NSLog(@"%s",__PRETTY_FUNCTION__);

    [self.tableView reloadData];

}

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {

    NSLog(@"%s",__PRETTY_FUNCTION__);
    UITableView *tableView = self.tableView;

    switch(type) {

        case NSFetchedResultsChangeInsert:
            [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeDelete:
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeUpdate:
            [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                                  withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeMove:
            [tableView deleteRowsAtIndexPaths:[NSArray
                                               arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            [tableView insertRowsAtIndexPaths:[NSArray
                                               arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}


- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id )sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {

    switch(type) {

        case NSFetchedResultsChangeInsert:
            [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeDelete:
            [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}


- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
    // The fetch controller has sent all current change notifications, so tell the table view to process all updates.
    [self.tableView endUpdates];
}

@end

TableView.h

表视图.h

@interface TableView : UITableView <UITableViewDataSource, UITableViewDelegate, NSFetchedResultsControllerDelegate>

@property (nonatomic, strong) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, strong) AppDelegate *del;
@property (nonatomic, strong) NSFetchedResultsController *controller;
@property (weak, nonatomic) IBOutlet TableView *tableView;

-(void)reloadTableView;
-(void)performControllerFetch;
@end

采纳答案by Alfie Hanssen

It looks like you are never setting the delegateand datasourceproperties on your tableview, no? You're implementing the methods in those protocols inside tableview.mbut not setting the two properties to selfhence calling reloadDatahaving no effect.

看起来您从未在 tableview 上设置delegatedatasource属性,不是吗?您正在内部实现这些协议中的方法,tableview.m但未将这两个属性设置为self因此调用reloadData无效。

Does this help?

这有帮助吗?

Edit:

编辑:

Looks like you have a tableViewproperty set on a subclass of UITableViewand hooked up to an interface builder file. This is an odd setup (a tableview within a tableview). Usually you would have something like a UIViewControllersubclass hooked up to an XIB and set an

看起来您tableView在 的子类上设置了一个属性UITableView并连接到接口构建器文件。这是一个奇怪的设置(tableview 中的 tableview)。通常你会有一个类似于UIViewController子类的东西连接到一个 XIB 并设置一个

@property (nonatomic, strong) IBOutlet UITableView * tableView

@property (nonatomic, strong) IBOutlet UITableView * tableView

in that subclass, or something similar. And then handle the data fetching and tableview delegate/datasource inside that viewcontroller subclass.

在那个子类中,或类似的东西。然后在该 viewcontroller 子类中处理数据获取和 tableview 委托/数据源。

But here, because of the nested UITableViews it's not as straightforward. Is there a reason for this design? I recommend moving to something like I describe above to help bring clarity to the setup.

但在这里,由于嵌套的UITableViews 并不那么简单。这种设计有原因吗?我建议转向我上面描述的东西,以帮助使设置更加清晰。

Also, try adding some NSLogstatements into your initmethod to see if the tableview != nil and the delegate and datasource properties have been set. My suspicion is that the connection is not being made.

此外,尝试NSLog在您的init方法中添加一些语句以查看 tableview != nil 以及委托和数据源属性是否已设置。我的怀疑是没有建立连接。

Also, unrelated to the problem at hand, you no longer have to manually @synthesize ivar = _ivar, it will be done for you automatically using the underscore convention.

此外,与手头的问题无关,您不再需要手动@synthesize ivar = _ivar,它将使用下划线约定自动为您完成。

回答by user3730468

I was having the same issue, that the reloadwas not working. I believe the delegatemethod I was calling it from was in a background threadwhich was causing the problem. My solution was to create a new method(below) and call the reloadfrom there instead, while also ensuring to call it from the main thread:

我遇到了同样的问题,重新加载不起作用。我相信我从中调用它的委托方法是在导致问题的后台线程中。我的解决方案是创建一个新方法(如下)并从那里调用重新加载,同时确保从主线程调用它:

- (void) tocLoad {
    dispatch_async(dispatch_get_main_queue(), ^{
    [self.tableView reloadData];
    });
}

回答by ashish mankar

If you are not able to reload table using

如果您无法使用重新加载表

[self.tableView reloadData];

Please make sure you are adding reference to following from the storyboard

请确保您正在从故事板中添加对以下内容的引用

@property (weak, nonatomic) IBOutlet TableView *tableView;

How to add reference you can click on following Image Link:

如何添加参考,您可以点击以下图片链接:

tableView reference outlet

tableView 参考插座

回答by Menno

This happened to me because there was a call to

这发生在我身上,因为有一个电话

tableView.beginUpdates()

without a call to

无需致电

tableView.endUpdates()

In my code. Removing tableView.beginUpdates()resolved the issue for me.

在我的代码中。删除tableView.beginUpdates()为我解决了这个问题。

回答by Max Friedman

I spent days debugging this issue, and found a ton of possible fixes. I starred the one that worked for me but they're all valid.

我花了几天时间调试这个问题,并找到了大量可能的修复方法。我出演了对我有用的那个,但它们都是有效的。

First, of course, check that your delegate, datasource, IBOutlet, etc are all configured properly.

首先,当然,检查您的委托、数据源、IBOutlet 等是否都配置正确。

**Set the All Exceptions breakpoint and see if you are getting an out of bounds exception in your datasource. Without setting the breakpoint there is no crash and the tableview will simply fail to reload.

**设置所有异常断点,看看是否在数据源中出现越界异常。不设置断点就不会崩溃,tableview 将无法重新加载。

Other possible fixes: check the tableview's frame to make sure the width or height is not 0. Try executing reloadData on the main thread. Lastly, try only reloading a single section (here).

其他可能的修复:检查 tableview 的框架以确保宽度或高度不为 0。尝试在主线程上执行 reloadData。最后,尝试仅重新加载单个部分(此处)。

Gotta love table views.

必须喜欢桌子的景色。

回答by Hussain Shabbir

Inspite of writing this [self.tableView reloadData];try below, becuase already you have written class so no need to take the outlet again:-

尽管在[self.tableView reloadData];下面写了这个尝试,因为你已经写好了课,所以不需要再去 插座了:-

[self reloadData]