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

ios - 无法将具有标识符标题的单元格出列


                                            <p><p>当我按下打开表格 View 的按钮时出现以下错误:</p>

<pre><code>Terminating app due to uncaught exception &#39;NSInternalInconsistencyException&#39;, reason: &#39;unable to dequeue a cell with identifier title - must register a nib or a class for the identifier or connect a prototype cell in a storyboard&#39;
</code></pre>

<p>这里是 tableview 的 ViewController 中的代码和导致问题的方法:</p>

<pre><code>- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

NSString *CellIdentifier = ;
UITableViewCell *cell = ;

return cell;
}
</code></pre>

<p>我研究了错误并尝试删除 <code>forIndexPath:indexPath</code>,因此代码如下所示:</p>

<pre><code>- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

NSString *CellIdentifier = ;
UITableViewCell *cell = ;

return cell;
}
</code></pre>

<p>现在这导致了一个新错误:</p>

<pre><code>Terminating app due to uncaught exception &#39;NSInternalInconsistencyException&#39;, reason: &#39;UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:
</code></pre>

<p>现在我做了一些日志记录,发现 <code>cell == nil</code> 是真的,所以我按照之前的一些问题的建议添加了一个检查:</p>

<pre><code>- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

NSString *CellIdentifier = ;
UITableViewCell *cell = ;
if(cell == nil){
   cell = [ init]
}

return cell;
}
</code></pre>

<p>现在这消除了所有错误,但是现在当我打开 tableview 时,当我想要在 Storyboard 中创建的单元格时,单元格是空的。</p>

<p>我该如何解决这个问题?</p>

<p>这是 Storyboard中 ViewController 的外观:
<img src="/image/TEvog.png" alt=""/> </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您可以在 UITable 上调用两种单元格回收方法
查看,</p>

<pre><code>-(UITableViewCell *) dequeueReusableCellWithIdentifier:forIndexPath:
-(UITableViewCell *)dequeueReusableCellWithIdentifier:
</code></pre>

<p>这有点令人困惑,但它们的使用方式却大不相同。采用第二个参数(类型为 NSIndexPath)的参数取决于您首先使用 tableView 注册了一个类或 xib 文件,以便 tableView 可以在没有方便的时候为您创建一个临时单元格回收。第一种方法将始终返回一个单元格,因此您可以编写自己的 cellForRowAtIndexPath: 代码。</p>

<p>第二种方法(只接受一个参数, (NSString *)cellIdentifier 可以并且将在没有方便回收的单元格时返回 nil。因此,当您使用此方法时,您应该测试结果为 nil 并创建一个单元格在这种情况下。
例如</p>

<pre><code>-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

static NSString *cellId = @&#34;cellID&#34;;

UITableViewCell *cell = ;

if (!cell) {
    cell = [initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
}

//etc etc decorate your cell...

return cell;
}
</code></pre>

<p>为了利用您的单元格原型(prototype),您需要为每一行/部分注册一个类或 xib,以便表格知道要创建哪个单元格。回收的东西只有在创建了足够的单元格来填满屏幕并且你开始滚动时才真正起作用。祝你好运</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 无法将具有标识符标题的单元格出列,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/27768917/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/27768917/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 无法将具有标识符标题的单元格出列