ios 带有两个自定义单元格的 UITableView(多个标识符)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14303832/
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
UITableView with two custom cells (multiple identifiers)
提问by MohamMad Salah
I am trying to put a cell as a space between each cell - which will be hidden by setting alpha = 0. In my table, the space cells will be for rows that are odd.
我试图将一个单元格作为每个单元格之间的空格 - 这将通过设置 alpha = 0 来隐藏。在我的表格中,空格单元格将用于奇数行。
Note that the actual cell height is 85, but the hidden cell height (ie space between cells) is 20.
请注意,实际单元格高度为 85,但隐藏单元格高度(即单元格之间的空间)为 20。
The problem is that the space cell height is 85, but not 20, I don't know why. Maybe the cell is not loaded correctly.
问题是空间单元高度是85,不是20,不知道为什么。也许单元格没有正确加载。
Cell
here is the UITableViewCell
- the actual cell - with identifier 'Cell'.
Cell
这是UITableViewCell
- 实际单元格 - 带有标识符“单元格”。
Cell2
is the space with identifier 'Space'.
Cell2
是带有标识符“Space”的空间。
Each class above has its own UITableViewCell
class and the XIB files are also assigned to each of them. The identifieris also set in the IB for each Xib.
上面的每个类都有自己的UITableViewCell
类,并且 XIB 文件也分配给每个类。该标识符也被在IB为每个XIB设置。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier1 = @"Cell";
static NSString *CellIdentifier2 = @"Space";
Cell *cell = (Cell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
if(!cell)
{
NSArray *ar = [[NSBundle mainBundle] loadNibNamed:@"CellView" owner:nil options:nil];
for (id obj in ar)
{
if ([obj isKindOfClass:[Cell class]])
{
cell = (Cell *)obj;
break;
}
}
}
if (indexPath.row % 2 == 1)
{
Cell2 *cell2 = (Cell2 *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
if (!cell2)
{
NSArray *ar = [[NSBundle mainBundle] loadNibNamed:@"Cell2" owner:nil options:nil];
for(id obj in ar)
{
if([obj isKindOfClass:[Cell2 class]])
{
cell2 = (Cell2 *)obj;
break;
}
}
// Method 1
cell2 = [[Cell2 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2];
// Method 2
//cell2 = [[Cell2 alloc] init];
// Method 3
//cell2 = (Cell2 *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
[cell2.contentView setAlpha:0];
// prevent selection and other stuff
[cell2 setUserInteractionEnabled:NO];
}
return cell2;
}
else
{
// Configure the actual cell
}
return cell;
}
}
回答by Attila H
* I've renamed some of your NIB/Class names for a better understanding. *
* 为了更好地理解,我重命名了您的一些 NIB/Class 名称。*
First, you should register each cells' NIB:
首先,您应该注册每个单元格的 NIB:
- (void)viewDidLoad{
[super viewDidLoad];
static NSString *CellIdentifier1 = @"ContentCell";
static NSString *CellIdentifier2 = @"SpaceCell";
UINib *nib = [UINib nibWithNibName:@"CellViewNIBName" bundle:nil];
[self.tableView registerNib:nib forCellReuseIdentifier:CellIdentifier1];
nib = [UINib nibWithNibName:@"CellSpaceNIBName" bundle:nil];
[self.tableView registerNib:nib forCellReuseIdentifier:CellIdentifier2];
self.contentView.hidden = YES;
[self loadData];
}
Because you have the NIBs registered, dequeueReusableCellWithIdentifier:
will always return a cell:
因为您已注册 NIB,所以dequeueReusableCellWithIdentifier:
将始终返回一个单元格:
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier1 = @"ContentCell";
static NSString *CellIdentifier2 = @"SpaceCell";
// Space Cell
if (indexPath.row % 2 == 1) {
CellSpace *cell = (CellSpace *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
return cell;
}
// Content cell
else {
CellView *cell = (CellView *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
// Configure cell
return cell;
}
}
Last, but not least, make sure to implement the following delegate method:
最后但并非最不重要的是,确保实现以下委托方法:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Space cell's height
if (indexPath.row % 2 == 1) {
return 20.0f;
}
// Content cell's height
else {
return 80.0f;
}
}
回答by Sandeep Jangir
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *returncell;
static NSString *cellIdentifier ;
if(indexPath.section == 0)
{
cellIdentifier = @"cell1";
}
else if (indexPath.section == 1)
{
cellIdentifier = @"cell2";
}
UITableViewCell *myCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
MapTableViewCell *myCustomCell = (MapTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(indexPath.section == 0)
{
if(myCell == nil)
{
myCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
getLocationBtn = [UIButton buttonWithType:UIButtonTypeCustom];
getLocationBtn.frame = CGRectMake(myCell.frame.origin.x,myCell.frame.origin.y+5 , 200, 30);
[getLocationBtn setTitle:@"your button title" forState:UIControlStateNormal];
[getLocationBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
[getLocationBtn addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
}
[myCell.contentView addSubview:getLocationBtn];
returncell = myCell;
}
else if (indexPath.section == 1)
{
if (myCustomCell == nil)
{
myCustomCell = [[MapTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
myCustomCell.nearbyLocation.text = @"demo Text";
returncell = myCustomCell;
}
return returncell;
}
//mycustom tablviewcell
//我的自定义表格视图单元格
import "MapTableViewCell.h"
导入“MapTableViewCell.h”
@implementation MapTableViewCell
@synthesize nearbyLocation;
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if(self)
{
self.backgroundColor = [UIColor groupTableViewBackgroundColor];
nearbyLocation = [[UILabel alloc]initWithFrame:CGRectMake(10, 5, 200, 30)];
[self addSubview:nearbyLocation];
}
return self;
}
@end
Best way to use number of custom cells with default cell
在默认单元格中使用自定义单元格数量的最佳方法
回答by John
In addition for the answers provided, I want to emphasize on the Cell Identifier for each different custom cells must be different too.
除了提供的答案之外,我想强调每个不同自定义单元格的单元格标识符也必须不同。
For example custom cellA
with identifier "Cell"
and custom cellB
with identifier "Cell2"
.
例如 custom cellA
with identifier"Cell"
和 custom cellB
with identifier "Cell2"
。