我有一个具有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];
}

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
}
有帮助吗?

解决方案

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

其他提示

您可以尝试对reloadData调用进行延迟 - 当我在重新排序单元格时尝试让我的tableview更新时遇到了类似的问题,但是如果我在调用reloadData期间应用程序崩溃,则会崩溃。

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

刷新方法:

    - (void)refreshDisplay:(UITableView *)tableView {
    [tableView reloadData]; 
}

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

[self performSelector:(@selector(refreshDisplay:)) withObject:(tableView) afterDelay:0.5];

希望它有效......

如果从调度方法中调用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];
    }); 
});

..方法形式相同:

-(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];
        }); 
}

您是否尝试在刷新方法中设置断点,以确保您的messages数组在调用reloadData之前具有正确的内容?

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top