mic*_*ica 9 cocoa-touch objective-c uitableview ios
我想用一个动态部分定义静态表视图这可能吗?
第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)
我对这个问题有部分解决方案
在Table视图数据源方法中,我返回静态单元格的超类结果,对于动态单元格,我返回所需的动态值.
在 - (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath dequeueReusableCellWithIdentifier:返回nil所以我创建了一个新的UITableViewCell
剩下的问题:
在xcode中你必须指定,"动态部分"中的行数(whitch是当然不是动态的).您不能显示超过此处定义的最大值(或获得例外;-)).
Samplecode:
- (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;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6351 次 |
| 最近记录: |