Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/121.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
如何在一个tableView中使用两个不同的自定义单元格?使用故事板,iOS 7_Ios_Iphone_Objective C_Uitableview_Custom Cell - Fatal编程技术网

如何在一个tableView中使用两个不同的自定义单元格?使用故事板,iOS 7

如何在一个tableView中使用两个不同的自定义单元格?使用故事板,iOS 7,ios,iphone,objective-c,uitableview,custom-cell,Ios,Iphone,Objective C,Uitableview,Custom Cell,我有两个自定义单元格。我想在我的UITableView中显示两个部分。第一部分有一行显示第一个自定义单元格,第二部分显示从核心数据中提取的对象列表 我应该如何实现“cellforrowatinexpath”方法 以下是我的一些代码: - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return the number of sections. return 2; } - (NSInteger)tabl

我有两个自定义单元格。我想在我的
UITableView
中显示两个部分。第一部分有一行显示第一个自定义单元格,第二部分显示从核心数据中提取的对象列表

我应该如何实现“cellforrowatinexpath”方法

以下是我的一些代码:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

// Return the number of sections.
return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

// Return the number of rows in the section.
if (section == 0) {
    return 1;

} else if (section == 1) {
   //gastos is an array
   return [self.gastos count];  
}
return 0;
}


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

switch (indexPath.section) {
    case 0:
    {
        SaldoCelda *cell1 = [tableView dequeueReusableCellWithIdentifier:@"Cell1"      forIndexPath:indexPath];
        return cell1;
    }
    case 1:
    {
        static NSString *CellIdentifier = @"Cell";
        CeldaGasto *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier      forIndexPath:indexPath];

        NSManagedObject *gasto = [self.gastos objectAtIndex:indexPath.row];

        [cell.monto setText:[NSString stringWithFormat:@"%@ AR$", [gasto valueForKey:@"monto"]]];
        [cell.categoria setText:[NSString stringWithFormat:@"%@", [gasto valueForKey:@"categoria"]]];
        [cell.fecha setText:[NSString stringWithFormat:@"%@", [gasto valueForKey:@"fecha"]]];

        return cell;
    }
    default:
        break;
}
return 0;
}
这是我收到的错误信息:

-[UITableView\u configureCellForDisplay:forIndexPath:],/SourceCache/UIKit\u Sim/UIKit-2903.23/UITableView.m:6246中的断言失败 2014-03-05 01:02:57.181支出[2897:70b]*由于未捕获的异常“NSInternalInconsistenceException”而终止应用程序,原因:“UITableView数据源必须从tableView返回单元格:cellForRowAtIndexPath:”


谢谢你的帮助

除了为一个单元格返回
nil
之外,在
numberofrowsinssection
中也有一个问题,第1节返回0行!!,虽然您希望在
单元格中有行,但请尝试此操作,因为我正在修改您的代码

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
if (section == 0) {
  return 1;
}
else if (section == 2) {
//gastos is an array
return [self.gastos count];  
}
//return 0; you should not write this because at the end of execution of this method it will return 0
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath         *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
switch (indexPath.section) {
case 0:
{
SaldoCelda *cell1 = [tableView dequeueReusableCellWithIdentifier:@"Cell1"      forIndexPath:indexPath];
cell=cell1;
    break; 
}
case 1:
{
static NSString *CellIdentifier = @"Cell";
CeldaGasto *cell2 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier      forIndexPath:indexPath];

NSManagedObject *gasto = [self.gastos objectAtIndex:indexPath.row];
[cell.monto setText:[NSString stringWithFormat:@"%@ AR$", [gasto valueForKey:@"monto"]]];
[cell.categoria setText:[NSString stringWithFormat:@"%@", [gasto valueForKey:@"categoria"]]];
[cell.fecha setText:[NSString stringWithFormat:@"%@", [gasto valueForKey:@"fecha"]]];
cell=cell2;
break;
}
default:
break;
}
return cell;
//return 0; remove this in your code
}

希望这将有助于

您可以在一个xib中创建一组单元格,并使用其标识符在CellForRowatinex中获取它。现在您可以设置它的属性和数据

static NSString *CellIdentifier = @"MyCellIdentifier";

MyCell *cell = (MyCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil];
cell = (MyCell *)[nib objectAtIndex:0];

 cell1=(MyCell *)[nib objectAtIndex:1];
}

我做了一个测试,效果很好。以下是步骤:

  • 在情节提要中创建UITableViewController
  • 将UITableViewCell拖放到已存在的单元格下方的UITableViewController上
  • 将CellIdentifier分配给两个单元格(我使用了Cell1和Cell2)
  • 创建UITableViewCell的2个子类(我称它们为Cell1和Cell2)
  • 创建UITableViewController的子类并以某种方式命名
  • 在cellForRowAtIndexPath方法中:

    static NSString *CellIdentifier = @"Cell1";
    
    static NSString *CellIdentifier1 = @"Cell2";
    
    switch (indexPath.section) {
    
        case 0:
        {
            Cell1 *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
            return cell;
        }
            break;
        case 1:
        {
            Cell2 *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1 forIndexPath:indexPath];
            return cell;
        }
            break;
    
        default:
            break;
    
    }

  • 正如您所看到的,实现与您的相同

    我复制错误的唯一方法是在开关块中返回nil,dequeueReusableCellWithIdentifier的文档中说:

    此方法始终返回有效的单元格

    即使你弄乱了你的手机识别码,你仍然不会得到你发布的错误。因此,我的结论是:


    重新启动、清理项目、重新启动模拟器或类似的操作会导致您的场景无法根据文档执行…

    阅读错误。您正在为您的
    cellforrowatinexpath:
    方法返回
    nil
    。要么是案例0,要么是案例1。使用调试器,看看哪一个是问题所在。顺便说一句,对于以
    return
    结尾的情况,在
    开关中不需要
    break
    语句。在这种情况下,
    break
    语句永远不会到达。你说得对!,在方法末尾返回0可以吗?切换结束后,
    实际返回0cellForRowAtIndexPath
    末尾的code>应该是
    返回nil返回0
    。必须检查我的答案并尝试执行
    numberOfRowsInSection:
    的实现,正如您所指出的,这与崩溃无关。但它确实将
    cellforrowatinexpath:
    中的问题缩小到
    案例0:
    code.hmmm,100%同意;)但还有一个问题他应该处理好。好吧,我只是解决了,但我的问题仍然存在。第0节只有我想要的一行,第1节有“gastos”计数。首先,表不是与您的问题无关的“静态单元格”内容(我想忽略了),其次,您需要为第2节创建一个原型单元格,并将其注册为“单元格”或者在您的
    cellforrowatinedexpath中初始化它。很高兴,如果单元格高度是另一回事,那么使用委托更改它:-(CGFloat)tableView:(UITableView*)tableView heightforrowatinedexpath:(NSIndexPath*)indexath{if(indexpathe.section==0&&indexpathe.row==0)返回50.0;返回100;}这些更改都不需要,而且实际上会使代码更加混乱。所有这些都无法解决问题。@rmaddy哪部分代码让您感到困惑?返回0;这意味着你将返回0到单元格和行…我不感到困惑。永远不会到达返回的
    。该表有两个部分。因此,
    开关的
    情况0
    情况1
    处理这两个部分。创建并返回一个单元格。永远不会达到默认值
    。永远不会到达方法末尾的
    返回值。OP的代码比你的建议干净多了。谢谢你,查兰,但我已经在之前的答案中解决了我的部分问题。我明白你所说的@rmaddy,返回值从未达到。@rmaddy当开关条件为真时,默认值从未达到。会吗?在第0节和第1节中,我将cell1分配给cell,cell2分配给cell,并在末尾将其作为返回单元格返回;break用于仅断开开关,而不是单元配置方法。所以它将到达返回单元格语句。如果我在这一点上错了,你能解释一下吗?我认为XIB文件的使用已经过时了,在xCode 5和iOS 7中,一切都与情节提要一起工作。实际上,我使用XIB文件来创建单元格,在进行单元格设计时,加载更简单,更专注。但是我知道你只想使用故事板,也许你还是应该尝试一下。你可以在故事板的帮助下做同样的事情。在故事板的情况下,您必须从故事板文件中获取单元的笔尖。
    
    return nil;