【发布时间】:2013-11-05 11:55:33
【问题描述】:
我是 iOS 新手。我有一个表格视图,我想在其中加载不同的自定义单元格。
这是我来自tableViewCellForRowAtIndexPath的代码:
if (self.mainTableView .tag==SEARCH)
{
static NSString *cellId=@"Cella";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:cellId];
if (cell == nil)
{
NSArray *topLevelOjects= [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
cell=[topLevelOjects objectAtIndex:0];
}
//code goes here
return cell;
}
else
{
static NSString *mCell = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:mCell];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
}
//code goes here
return cell;
}
在方法 numberOfRowsInSections:(UITableView *) tableview 我有:
if (tableView.tag==SEARCH)
{
return [firstList count];
}
else
{
return [secondList count];
}
我遇到的问题是每次执行 ELSE 时,tableview 都包含第一个和第二个 CustomCell。为什么?
【问题讨论】:
-
这段代码是在哪里实现的?在
tableView:cellForRowAtIndexPath:?你在tableView:numberOfRowsInSection:返回什么? -
我建议把
if(self.mainTableView.tag==SEARCH)改成if(tableView.tag == SEARCH)。那你什么时候放标签呢? -
如果单元格的高度不同,您还必须实现
heightForRowAtIndexPath:,否则它们会重叠。 -
我已经更新了我的问题。谢谢
-
单元格的大小确实不同
标签: ios tableview custom-cell