我知道你不能将静态和动态细胞类型混合在一起,UITableView但我想不出更好的方式来描述我的问题.
我有几个具有固定内容的预定单元格,我还有未知数量的具有动态内容的单元格位于中间.所以我想让我的表看起来像这样:
Fixed
Fixed
Fixed
Dynamic
Dynamic
Dynamic
Dynamic
Dynamic
Fixed
Fixed
Run Code Online (Sandbox Code Playgroud)
那你怎么建议我在我的cellForRowAtIndexPath方法中解决这个问题?
谢谢.
用故事板,静态细胞,cellForRowAtIndexPath:行
UITableViewCell *cell =
[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Run Code Online (Sandbox Code Playgroud)
总是回来nil.
我检查了以下内容:
我的视图控制器UITableViewController当然是包含在故事板中的临时导航控制器中的子类.怀疑我的视图控制器以某种方式不知道故事板中定义的单元标识符,因为它可能是另一个实例,这里是代码"实例化"它.在prepareForSegue:,我用
CustomViewController *vc = [[[segue destinationViewController]
viewControllers] objectAtIndex:0];
Run Code Online (Sandbox Code Playgroud)
这里完成的视图控制器的其他自定义(设置属性等)工作正常.
我使用静态单元格,因为节和行的数量不会改变,每个单元格包含静态文本(以及其他控件或要编辑的文本字段).
在我看来,这是一个非常常见的任务(在视图控制器的数据源方法中自定义故事板中的静态单元格).我究竟做错了什么?
我想用一个动态部分定义静态表视图这可能吗?
第0部分应是静态的,标签用xcode连接到出口.
第1节应是动态的
我试过这个,但是我不知道我将为静态部分返回什么单元格.
static NSString *CellIdentifier = @"ItemCellBasic";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
switch (indexPath.section)
{ case 0:
return // I don´t know what
case 1:
cell.textLabel =@"dynamic";
return cell;
}
Run Code Online (Sandbox Code Playgroud)
编辑1; 现在我试过了:
case 0: return [super tableView:tableView cellForRowAtIndexPath:indexPath];
Run Code Online (Sandbox Code Playgroud)
但得到了:
*** Assertion failure in -[UITableView _createPreparedCellForGlobalRow:withIndexPath:], /SourceCache/UIKit_Sim/UIKit-1912.3/UITableView.m:6072
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
Run Code Online (Sandbox Code Playgroud) 我试图在分组表格视图中混合动态和静态单元格:我希望在顶部有两个静态单元格,然后是动态单元格的一部分(请参阅下面的屏幕截图).我已将表视图内容设置为静态单元格.

基于AppleFreak的建议,我更改了我的代码如下:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell;
if (indexPath.section <= 1) { // section <= 1 indicates static cells
cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
} else { // section > 1 indicates dynamic cells
CellIdentifier = [NSString stringWithFormat:@"section%idynamic",indexPath.section];
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
}
return cell;
}
Run Code Online (Sandbox Code Playgroud)
但是,我的应用程序崩溃并显示错误消息
由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'UITableView dataSource必须从tableView返回一个单元格:cellForRowAtIndexPath:'
对于第0部分和第0行.从cell = [super tableView:tableView cellForRowAtIndexPath:indexPath]第0部分和第0行返回的单元格是nil.
我的代码出了什么问题?我的网点可能有问题吗?我没有设置任何出口,因为我是子类,UITableViewController并且据说不设置tableview的任何出口(?).有关如何更好地做到这一点的任何建议?

我在故事板中重新创建了我的场景(请参阅我上面更新的屏幕截图)并重新编写视图控制器以便从新基础开始.我也在Apple的论坛中阅读了applefreak建议的描述.但是,我运行的第一个问题是使用该方法 …