代码之家  ›  专栏  ›  技术社区  ›  Dutchie432

[UITableView重载数据]有什么用?

  •  22
  • Dutchie432  · 技术社区  · 17 年前

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

    奇怪的是,当我上下滚动我的UITableView时,委托方法最终被调用,我的节标题也会发生变化(它们显示了该节的消息计数)。他们不应该实时更新,而不是等待我的滚动触发刷新吗?此外,新单元格从未添加到节中!!

    [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
    }
    
    4 回复  |  直到 17 年前
        1
  •  29
  •   smorgan    17 年前

    作为健全性检查,您是否验证了表在该点上不是零?

        2
  •  12
  •   h4xxr    17 年前

    你可以尝试延迟reloadData调用-我在重新排列单元格时尝试更新tableview时遇到了类似的问题,除了在调用reloadData时应用程序崩溃。

    所以像这样的东西可能值得一试:

    刷新方法:

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

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

        3
  •  6
  •   nooitaf    14 年前

    如果从已调度方法内调用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];
            }); 
    }
    
        4
  •  0
  •   Marc Charbonneau    17 年前