TableView包含多个原型单元格 [英] TableView with multiple prototype cells
问题描述
我有一个关于具有3种不同原型单元的表视图的简单问题。前两个只发生一次,而第三个发生4次。现在我很困惑的是如何在我的cellforRowatindexpath中指定哪个单元原型用于哪一行。所以,我想要第0行,使用原型1,行1,使用原型2,行3,4,5和6使用原型3.最好的方法是什么?我是否给每个原型一个标识符,然后使用dequeueReusableCellWithIdentifier:CellIdentifier?
你能提供一些示例代码吗?
I had a simple question regarding a table view with 3 different kinds of prototype cells. The first two occur just once while the third occurs 4 times. Now what I'm confused about is how to specify in my cellforRowatindexpath which cell prototype to use for which row. So, I want something like for row 0, use prototype 1, for row 1, use prototype 2, for rows 3,4,5 and 6 use prototype 3. What's the best way to do this? Do i give each prototype an identifier and then use dequeueReusableCellWithIdentifier:CellIdentifier ? Can you'll provide some sample code?
编辑:
仍然无效。这是我目前的代码。 (我只有一个案例用于交换机状态,因为我只想测试并查看是否在第一行中生成了单元格,但当前表格视图为空白)
Still not working. This is the code I have at the moment. ( I only have one case for the switch statment because I just want to test and see if the cell is being generated in the first row or not, but currently table view is blank)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
switch(indexPath.row)
{
case 0: {static NSString *CellIdentifier = @"ACell";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:@"ACell"];
if(cell==nil) {
cell=[[UITableViewCell alloc]
initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"ACell"];
}
return cell;
break;
}
}
}
Acell是我的标识符我创建的单元原型。我
Acell is my identifier for a cell prototype that I created. I
推荐答案
如果您使用的是三个原型,那么请使用三个标识符。因为只有一个标识符会导致问题。你会得到错误的结果。所以这样的代码。
If you are using three prototype then use three identifiers. Because only one identifier will cause problem. And you will get wrong result. So code like this.
if(indexPath.row==0){
// Create first cell
}
if(indexPath.row==1){
// Create second cell
}
else{
// Create all others
}
您也可以在这里使用switch case以获得最佳性能。
You can use switch case also here for best performance.
这篇关于TableView包含多个原型单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!