ios 具有多个原型单元格的 TableView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14758756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TableView with multiple prototype cells
提问by David West
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?
我有一个关于具有 3 种不同原型单元格的表格视图的简单问题。前两个只出现一次,而第三个出现 4 次。现在我感到困惑的是如何在我的 cellforRowatindexpath 中指定哪个单元格原型用于哪一行。所以,我想要像第 0 行,使用原型 1,第 1 行,使用原型 2,第 3、4、5 和 6 行使用原型 3。这样做的最佳方法是什么?我是否给每个原型一个标识符,然后使用 dequeueReusableCellWithIdentifier:CellIdentifier ?你能提供一些示例代码吗?
EDIT:
编辑:
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)
还是行不通。这是我目前拥有的代码。(我只有一个 switch 语句的案例,因为我只想测试并查看单元格是否在第一行中生成,但当前表视图是空白的)
- (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 is my identifier for a cell prototype that I created. I
Acell 是我创建的单元原型的标识符。一世
回答by aks.knit1108
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
}
You can use switch case also here for best performance.
您也可以在此处使用 switch case 以获得最佳性能。
回答by Ashish Pisey
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell.tag == 0)
{
return array1.count;
}
else(cell.tag == 1)
{
return array2.count;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier;
NSString *membershipType = [membershipTypeArray objectAtIndex:indexPath.row];
if ([membershipType isEqualToString:@"silver"]||[membershipType isEqualToString:@"gold"])
{
cellIdentifier = @"cell";
}
else if ([membershipType isEqualToString:@"platinum"])
{
cellIdentifier = @"premiumCustomCell";
cell.customCellImageView.image = [cellImageArray objectAtIndex:indexPath.row];
}
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.headingLabel.text = [titleArray objectAtIndex:indexPath.row];
}
回答by Mannam Brahmam
Here i wrote code like:
在这里,我编写了如下代码:
#pragma mark == Tableview Datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSInteger nRows = 0;
switch (section) {
case 0:
nRows = shipData.count;
break;
case 1:
nRows = dataArray1.count;
break;
default:
break;
}
return nRows;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *cellIdentifier = @"cellIdentifier1";
NSString *cellIdentifier1 = @"cellIdentifier2";
SingleShippingDetailsCell *cell;
switch (indexPath.section) {
case 0:
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
//Load data in this prototype cell
break;
case 1:
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier1];
//Load data in this prototype cell
break;
default:
break;
}
return cell;
}