Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone UITableViewCell为零_Iphone_Objective C_Ios_Uitableview - Fatal编程技术网

Iphone UITableViewCell为零

Iphone UITableViewCell为零,iphone,objective-c,ios,uitableview,Iphone,Objective C,Ios,Uitableview,我相信这个问题有一个足够简单的答案,但我似乎找不到。我的UITableViewController中有以下代码: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView de

我相信这个问题有一个足够简单的答案,但我似乎找不到。我的
UITableViewController
中有以下代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell==nil)
    {
        NSLog(@"Cell is nil!");
    }

    return cell;
}
但是在我的日志输出中

手机是零

紧接着

由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“UITableView数据源必须从tableView返回单元格:CellForRowatineXpath:” *第一次抛出调用堆栈: (0x1b68d72 0x135fe51 0x1b68bd8 0xb0e315 0xcb373 0x63578 0xcb1d3 0xcff19 0xcffcf 0xb9384 0xc952e 0x691bc 0x13736be 0x215c3b6 0x2150748 0x215055c 0x20ce7c4 0x20cf92f 0x2171da2 0x1b4b4 0x1be63 0x2c2be 0x2cf9f 0x1f3fd 0x1AC59 0x1AC510 0x1A12 0x1b0fb46 0x1BEDED4 0x1BED28F 0x2471) libc++abi.dylib:terminate调用引发异常 (lldb)

有人知道为什么会发生这种情况,更重要的是,知道如何解决它吗


提前感谢。

如果单元格为空,则必须创建单元格,因为方法为空

[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
从表视图缓存返回未使用的单元格

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:NSIndexPath*)indexPath
{
   static NSString *CellIdentifier = @"Cell";
   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
   if (cell == nil)
   {
       cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
   }

   //configure cell

   return cell;
}

dequeueReusableCellWithIdentifier
返回一个已创建的单元格,其中包含您传入的标识符(如果存在可重用的标识符)

如果没有,你负责创建一个

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

是,方法
UITableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier]
表示它将重用已分配了CellIdentifier的单元格。但您尚未分配任何具有此标识符的单元格,对于tableView,它将为屏幕分配一些单元格,然后它将重用具有标识符的单元格,如果您这样做,您会发现它可以工作

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell==nil)
    {
        NSLog(@"Cell is nil!");
        cell = [[[UITableViewCell alloc] initWithSyle:UITableViewCellStyleNormal reuseIdentifier:CellIdentifier] autorelease];
    }

    return cell;
}

做一个测试。

你是否使用
接口生成器来完成这个类?如果是这样,请不要忘记从表视图中进行引用。我有同样的零问题,这是我的情况