菜鸟教程小白 发表于 2022-12-13 06:05:33

ios - 没有足够的单元格在 UITableView 中使用


                                            <p><p>我在 Storyboard 中创建了一个 UITableView,它是动态单元格。问题是当没有足够的单元格可以重复使用时,它会随机清空我的一些单元格。我认为这是合乎逻辑的,但我想解决这个问题。</p>
<p>我举个例子:
我有一个 UITableView 能够在一个 View 中生成 10 个单元格。但是现在,我只想显示 10 个单元格中的 8 个单元格。当我只有一个部分时,它没有问题。超过 1 个部分,它总是会清空 2 个单元格并显示 8 个单元格,但它应该显示 10 个单元格。</p>
<p>谁能帮我解决这个问题?谢谢。</p>
<p><strong>已更新源代码</strong></p>
<pre><code>#pragma mark - List View
#pragma mark - Table View Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
   return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:   (NSInteger)section {
   // Number of rows is the number of time zones in the region for the          specified section.
return self.listCollection.count;
}

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

   UITableViewCell *cell = ;

   for (id object in cell.contentView.subviews) {
       ;
   }

   ];

   return cell;

}
</code></pre>
<p><strong>self.listCollection</strong> 有一个 UIView 对象数组。</p>
<p><strong>已更新图片:</strong></p>
<ol>
<li> <a href="https://www.dropbox.com/s/4hzzistkd8vtc36/Screen%20Shot%202015-03-26%20at%202.29.52%20PM.png?dl=0" rel="noreferrer noopener nofollow">Image 1</a> </li>
<li> <a href="https://www.dropbox.com/s/u1vfwfsxgx546se/Screen%20Shot%202015-03-26%20at%202.30.01%20PM.png?dl=0" rel="noreferrer noopener nofollow">Image 2</a> </li>
</ol></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>发生这种情况是因为您使用了 2 个部分,但没有分别指定每个部分的内容。要理解这一点,我们需要查看 <a href="https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/#//apple_ref/occ/instm/UIView/addSubview:" rel="noreferrer noopener nofollow">Apple Documentation</a> 中 <code>addSubview:</code> 方法的描述。 </p>
<blockquote>
<p>This method establishes a strong reference to view and sets its next responder to the receiver, which is its new superview.</p>
<p>Views can have only one superview. If view already has a superview and that view is not the receiver, <strong>this method removes the previous superview before making the receiver its new superview</strong>.</p>
</blockquote>
<p>仔细查看第二段中的粗体部分。由于您使用 <code>listCollection</code> 中的相同 View 对象来填充这两个部分,因此最新创建的单元格将成为该 View 对象的 <code>superview</code> 并且前一个单元格将被省略只不过是飞机<code>contentView</code>。您可以通过为单元格 <code>contentView</code> 指定一些默认颜色来获得真实的感觉。您的空白单元格将显示完整内容的颜色,而其他单元格将显示 <em>view</em></p>
<pre><code>cell.contentView.backgroundColor = ;
</code></pre>
<p><strong>解决方案</strong><br/>
我将建议对这两个部分使用 2 个不同的数据源,如下所述。</p>
<pre><code>-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:   (NSInteger)section {
   // Number of rows is the number of time zones in the region for the          specified section.
   if(section==0)
      return self.listCollection.count;
   else   
      return &lt;row count for section 1&gt;
}
</code></pre>
<p>您需要为每个部分修改 <code>cellForRowAtIndexPath:</code> 代码。</p>
<pre><code>-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

   UITableViewCell *cell = ;

   for (id object in cell.contentView.subviews) {
       ;
   }
   if(section==0)
      ];
   else
      ];

   return cell;
}
</code></pre>
<p>但是,如果您必须使用相同的数组,那么您可以使用这种技术。感谢 <a href="https://stackoverflow.com/questions/10614276/clone-or-copy-uiviewcontroller-or-uiview" rel="noreferrer noopener nofollow">Rishi</a>为此。<br/>
最后但同样重要的是,您应该使用 <em>Michal Zygar</em> 在他的帖子中建议的自定义 <code>UITableViewCell</code> 类。</p>
<pre><code>UIView *tempView = ;
NSData *tempArchiveView = ;
UIView *viewOfSelf = ;
;
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 没有足够的单元格在 UITableView 中使用,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/29271680/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/29271680/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 没有足够的单元格在 UITableView 中使用