什么是[UITableView reloadData]?

Dut*_*432 22 iphone cocoa-touch uitableview

我有一个具有UITableView的应用程序.这个UITableView由在appDelegate中保存(作为属性)的NSMutableArray填充.您可以将其视为电子邮件窗口.它列出了子类UITableViewCell中的消息.当出现新消息时,我完成了下载消息的所有代码,将数据添加到appDelegate的NSMutableArray中,该消息包含所有消息.这段代码工作正常.

现在,一旦下载新消息并将其添加到数组中,我就会尝试使用以下代码更新我的UITableView,但是UITableView的委托函数不会被调用.

奇怪的是当我上下滚动我的UITableView时,委托方法最终被调用,我的节标题也会改变(它们显示该节的消息计数).他们不是实时更新而不是等待我的滚动触发刷新?此外,新细胞永远不会添加到该部分!!

请帮忙!!

APPDELEGATE CODE:

[self refreshMessagesDisplay]; //This is a call placed in the msg download method

-(void)refreshMessagesDisplay{
    [self performSelectorOnMainThread:@selector(performMessageDisplay) withObject:nil waitUntilDone:NO];
}

-(void)performMessageDisplay{
    [myMessagesView refresh];
}
Run Code Online (Sandbox Code Playgroud)

UITableViewController代码:

-(void) refresh{
    iPhone_PNPAppDelegate *mainDelegate = (iPhone_PNPAppDelegate *)[[UIApplication sharedApplication] delegate];

    //self.messages is copied from appDelegate to get (old and) new messages.
    self.messages=mainDelegate.messages;

    //Some array manipulation takes place here.

    [theTable reloadData];
    [theTable setNeedsLayout];  //added out of desperation
    [theTable setNeedsDisplay];  //added out of desperation
}
Run Code Online (Sandbox Code Playgroud)

smo*_*gan 29

作为一个完整性检查,您是否已经确认该表在那时不是零?

  • 因为我不是我自己的英雄,就像你现在一样.我有很多东西需要学习!! 谢谢你们!! (9认同)
  • 0x0实际上是nil--表视图当然存在,但是你的指针没有正确设置.仔细检查你的笔尖. (7认同)
  • 为什么不使用self.tableView而不是维护对表的单独引用? (4认同)

h4x*_*xxr 12

您可以尝试对reloadData调用进行延迟 - 当我在重新排序单元格时尝试让我的tableview更新时,我遇到了类似的问题,除非应用程序崩溃,如果我在其中调用reloadData.

所以这样的事情可能值得一试:

刷新方法:

    - (void)refreshDisplay:(UITableView *)tableView {
    [tableView reloadData]; 
}
Run Code Online (Sandbox Code Playgroud)

然后用(比方说)延迟0.5秒来调用它:

[self performSelector:(@selector(refreshDisplay:)) withObject:(tableView) afterDelay:0.5];
Run Code Online (Sandbox Code Playgroud)

希望它有效......


noo*_*taf 6

如果从调度方法中调用reloadData,请确保在主队列上执行它.

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND,0), ^(void) {

    // hard work/updating here

    // when finished ...
    dispatch_async(dispatch_get_main_queue(), ^(void) {
        [self.myTableView reloadData];
    }); 
});
Run Code Online (Sandbox Code Playgroud)

..方式形式:

-(void)updateDataInBackground {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND,0), ^(void) {

        // hard work/updating here

        // when finished ...
        [self reloadTable];
    });
}

-(void)reloadTable {
       dispatch_async(dispatch_get_main_queue(), ^(void) {
            [myTableView reloadData];
        }); 
}
Run Code Online (Sandbox Code Playgroud)