我想用一个动态部分定义一个静态 TableView 这可能吗?
第 0 部分应该是静态的,标签在 xcode 中与 socket 连接。
第 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;
}
编辑 1; 现在我试过了:
case 0: return [super tableView:tableView cellForRowAtIndexPath:indexPath];
但是得到了:
*** 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:'
最佳答案
这个问题我有部分解决方案
在 TableView 数据源方法中,我为静态单元格返回父类(super class)结果,为动态单元格返回所需的动态值。
在 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 中 dequeueReusableCellWithIdentifier: 返回 nil 所以我创建了一个新的 UITableViewCell
剩下的问题:
在 xcode 中,您必须指定“动态部分”中的行数(当然不是动态的)。你不能显示超过你在这里定义的最大值(或得到一个异常(exception);-))。
示例代码:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
switch (section)
{ case STATIC_SECTION:
return [super tableView:tableView numberOfRowsInSection:section];
case DYNAMIC_SECTION
return NUMBER_OF_DYNAMIC_ROWS;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"ItemCellBasic";
UITableViewCell *cell;
switch (indexPath.section)
{
case STATIC_SECTION:
return [super tableView:tableView cellForRowAtIndexPath:indexPath];
case DYNAMIC_SECTION:
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
{ cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
}
cell.textLabel.text=DYNAMIC_TEXT;
return cell;
}
}
关于objective-c - 在具有 "dynamic prototypes"的静态 Tableview 中包含具有 "static cells"的部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9301537/