在UITableView中显示多个自定义单元格? [英] Display multiple custom cells in a UITableView?
问题描述
我正在SnowLeopard上使用Xcode 4.2,而我的项目正在使用情节提要.我正在尝试使用2种不同的自定义单元格类型sessionCell
和infoCell
来实现UITableView
.我可以使这两种类型出现在同一列表中,但是现在我有一个新问题? sessionCell
仅显示一次,然后显示infoCells
的X数-正如我想要的一样-除了第一个infoCell
总是被sessionCell
覆盖!
I am using Xcode 4.2 on SnowLeopard, and my project is using storyboards. I am trying to implement a UITableView
with 2 different custom cell types, sessionCell
and infoCell
. I can get the 2 types to appear within the same list, but now I have a new problem?! The sessionCell
is displayed once, and then X number of infoCells
are displayed after it - just as I wanted - except that the first infoCell
is always overwritten by the sessionCell
!
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return [self.people count];
}
//inside cellForRowAtIndexPath
if(indexPath.row == 0) {
cell = [tableView dequeueReusableCellWithIdentifier:@"sessionCell"];
} else {
cell = [tableView dequeueReusableCellWithIdentifier:@"infoCell"];
}
...
return cell;
我试图说return array count + 1
,甚至是硬编码的return 7
(符合我的示例),但是两者都不正确!
I've tried to say return array count + 1
, or even hardcoded return 7
(it fits my example) but both are incorrect!
myObject *person = [self.people objectAtIndex:indexPath.row];
还是我的问题出在上述那行?我什至尝试过indexPath.row+1
...
Or does my problem lie in the above line? I've even tried indexPath.row+1
...
任何帮助将不胜感激!
推荐答案
如果我正确理解了您的问题,则第一个infoCell
(第二个UITableView
行)应该显示第一人称对象的数据,对吗?
If I understand your question correctly, the first infoCell
(second UITableView
row) should display the first person object's data, right?
那么看来您想要:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *sessionCellID = @"sessionID";
static NSString *infoCellID = @"infoID";
if( indexPath.row == 0 ) {
SessionCellClass *cell = nil;
cell = (SessionCellClass *)[tableView dequeueReusableCellWithIdentifier:sessionCellID];
if( !cell ) {
// do something to create a new instance of cell
// either alloc/initWithStyle or load via UINib
}
// populate the cell with session model
return cell;
}
else {
InfoCellClass *cell = nil;
cell = (InfoCellClass *)[tableView dequeueReusableCellWithIdentifier:infoCellID];
if( !cell ) {
// do something to create a new instance of info cell
// either alloc/initWithStyle or load via UINib
// ...
// get the model object:
myObject *person = [[self people] objectAtIndex:indexPath.row - 1];
// populate the cell with that model object
// ...
return cell;
}
}
,您需要返回[[self people] count] + 1
作为行数:
and you need to return [[self people] count] + 1
for the row count:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[self people] count] + 1;
}
以便第n行显示第(n-1)个数据.
so that the n'th row shows the (n-1)th data.
这篇关于在UITableView中显示多个自定义单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!