菜鸟教程小白 发表于 2022-12-12 17:42:00

iphone - 重新加载 UITableView 数据而不重新加载 View ?


                                            <p><p>我有一个 <code>UITableView</code>,其中包含一堆项目,每次通过 <code>refreshRows</code> 方法加载表时,我都会从 Web 应用程序获取这些项目的状态。完成此操作后,我重新加载表。</p>

<p>当我向表中添加项目时,我发现自己收到一条消息“无效更新:部分中的行数无效”。重新加载表中的数据是必要的,所以我将旧的 <code>viewDidAppear</code> 方法更改为新的方法(如下所示)。我现在有两个 <code>reloadData</code> 调用,它们都刷新了我的 View 。</p>

<p>问题:有没有更清洁的方法来做到这一点?添加后我需要重新加载我的数据,但我不希望在从网络获取所有状态之前重新加载 View 。</p>

<pre><code>- (void)viewDidAppear:(BOOL)animated // new
{
    NSLog(@&#34;viewDidAppear&#34;);
    ;

    ; // &lt;------------- Prettier way to do this?
    ;
    ;

}

- (void)viewDidAppear:(BOOL)animated // old
{
    NSLog(@&#34;viewDidAppear&#34;);
    ;

    ;
    ;

}

- (void)refreshRows {
    // foreach row get status from webapp
}
</code></pre>

<p>编辑:</p>

<p>这是请求的代码:</p>

<pre><code>- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    id &lt;NSFetchedResultsSectionInfo&gt; sectionInfo =
    [ objectAtIndex:section];
    return ;
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>如果只有您的数据源知道更改(并且应该注意这一点),您可能会尝试这样做:</p>

<ol>
<li>注册您的 TableView 以观察数据源更新的通知中心。</li>
<li>从数据源向 NSNotificationCenter 发布更新已经到来的通知。</li>
<li>使用 响应 TableView 中的更新;</li>
</ol>

<p>在数据源中:</p>

<pre><code>- (void) dataSourceChanged
{

    // All instances of TestClass will be notified
    [
      postNotificationName:@&#34;myUniqueDataSourceChanged&#34;
      object:self];

}
</code></pre>

<p>在 TableViewController 中:</p>

<pre><code>- (void)viewDidAppear:(BOOL)animated // new
{
    NSLog(@&#34;viewDidAppear&#34;);
    ;

    ; // &lt;------------- Prettier way to do this?
    ;
    [ addObserver:self
      selector:@selector(receiveNotification:)
      name:@&#34;myUniqueDataSourceChanged&#34;
      object:self.dataSource];
}

- (void) receiveNotification:(NSNotification *) notification
{
    if ([ isEqualToString:@&#34;myUniqueDataSourceChanged&#34;])
      ;
}

- (void) dataSourceHasBeenChanged
{

    ;
    ;
}
</code></pre>

<p>这将在每次更新数据源时自动更新您的表格 View </p></p>
                                   
                                                <p style="font-size: 20px;">关于iphone - 重新加载 UITableView 数据而不重新加载 View ?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/7722711/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/7722711/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: iphone - 重新加载 UITableView 数据而不重新加载 View ?