簡體   English   中英

iOS 7 TableView出隊無法正常工作

[英]iOS 7 TableView dequeue not working

我有一個非常煩人的問題,我不確定為什么會這樣。

我已經設置了“表格”視圖,在單元格中具有正確的標識符,但是它仍然給我“無法使具有標識符單元格的單元出隊-必須為該標識符注冊一個筆尖或一個類,或者在情節提要中連接原型單元格”當我運行它時。

代碼看起來像這樣

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    NSDictionary *event = [self.events objectAtIndex:indexPath.row];
    cell.textLabel.text = [event objectForKey:@"name"];

    return cell;
}

任何幫助都將是很大的,因為我想撕開頭發。 該表已連接到該類,然后Cell具有該@“ Cell”標識符

重新檢查所有4個步驟



步驟1:您的cellForRowAtIndexPath應該看起來像這樣

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier   forIndexPath:indexPath];

if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
}

NSDictionary *event = [self.events objectAtIndex:indexPath.row];
cell.textLabel.text = [event objectForKey:@"name"];

return cell;
}



步驟2:您的“ ViewController.m”文件應如下所示

#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

@end



步驟3:請檢查-單擊視圖控制器,移至連接檢查器-查看此檢查器中是否還有其他不需要的連接。

您的控制器檢查員應該這樣



步驟4: Tableview單元屬性檢查器應為[]

您的Tableview單元格屬性檢查器應如下所示

代替:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

嘗試:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

如果那之后不起作用

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

加:

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
}

正確,您需要注冊一個單元以供重用。 創建表視圖后,可以通過編程方式完成此操作:

static NSString *cellIdentifier = @"Cell";
[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellIdentifier];

或者,也可以在Interface Builder中通過選擇單元格並導航到屬性檢查器並更改單元格的ID來完成此操作。

您不檢查是否分配了單元格。您可以嘗試以下代碼。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    // add a placeholder cell while waiting on table data

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];
    } 
}

在文檔大綱,驗證您的原型UITableViewCell是一個孩子UITableView 情節提要文檔大綱

在“屬性檢查器”中,驗證是否在情節提要中正確設置了“單元格標識符”和“單元格樣式”:

表格視圖單元格屬性檢查器的情節提要截圖

在身份檢查器中,驗證是否已將類設置為UITableViewCell 表視圖單元格身份檢查器的情節提要截圖

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = [NSString stringWithFormat:@"cell %d",indexPath.row];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell = nil;

    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

        NSDictionary *event = [self.events objectAtIndex:indexPath.row];
        cell.textLabel.text = [event objectForKey:@"name"];

    }

    return cell;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM