ios tableViewCell.xib 文件生成多个单元格
ios tableViewCell.xib file generate more than one cell
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView registerNib:[UINib nibWithNibName:@"FriendTableViewCell" bundle:nil] forCellReuseIdentifier:@"FIDCELL"];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
FriendTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FIDCELL"];
return cell;
}
此代码块有效,但当我在 FriendTableViewCell.xib 中添加新单元格时实际上不起作用,我无法调用新单元格。我怎么称呼这个细胞或者这是可能的?如果这个问题不清楚我可以添加图片...
**
i try to call different cell from same .xib
**
错误:
我找到了解决方案我生成新的 .xib 并在页面加载时调用它们
[self.tableView registerNib:[UINib nibWithNibName:@"FriendTableViewCell" bundle:nil] forCellReuseIdentifier:@"FIDCELL"];
[self.tableView registerNib:[UINib nibWithNibName:@"Cell2" bundle:nil] forCellReuseIdentifier:@"FIDCELL2"];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellID = @"";
if (indexPath.row % 2 == 0) {
cellID = @"FIDCELL";
} else {
cellID = @"FIDCELL2";
}
FriendTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
// Configure the cell...
cell.nameLabel.text = self.namesArray[indexPath.row];
cell.photoImageView.image = [UIImage imageNamed:self.photoNameArray[indexPath.row]];
return cell;
}
此代码部分完全有效。
好吧,首先,每个单元格都应该有其唯一标识符,所以如果第一个单元格有一个名为 "FIDCELL" 的标识符,第二个单元格应该有另一个名为 "FIDCELL2" 的标识符,然后删除您的 viewDidLoad 中的以下行:
[self.tableView registerNib:[UINib nibWithNibName:@"FriendTableViewCell" bundle:nil] forCellReuseIdentifier:@"FIDCELL"];
并将其添加到 cellForRowAtIndexPath
-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomTableViewCell * cell ;
if(condtion1)
{
cell = [tableView dequeueReusableCellWithIdentifier:firstCellIdentfier];
}
else if(condtion2)
{
cell = [tableView dequeueReusableCellWithIdentifier:secondCellIdentfier];
}
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:yourNibName owner:self options:nil];
if(condtion1)
{
//0 is the index of the first cell in the nib
cell = [nib objectAtIndex:0];
}
else if(condtion2)
{
//1 is the index of the second cell in the nib .
cell = [nib objectAtIndex:1] ;
}
}
//do other work
return cell ;
}
其中条件 1 和条件 2 是决定选择哪个单元格的条件。
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView registerNib:[UINib nibWithNibName:@"FriendTableViewCell" bundle:nil] forCellReuseIdentifier:@"FIDCELL"];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
FriendTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FIDCELL"];
return cell;
}
此代码块有效,但当我在 FriendTableViewCell.xib 中添加新单元格时实际上不起作用,我无法调用新单元格。我怎么称呼这个细胞或者这是可能的?如果这个问题不清楚我可以添加图片...
**
i try to call different cell from same .xib
**
错误:
我找到了解决方案我生成新的 .xib 并在页面加载时调用它们
[self.tableView registerNib:[UINib nibWithNibName:@"FriendTableViewCell" bundle:nil] forCellReuseIdentifier:@"FIDCELL"];
[self.tableView registerNib:[UINib nibWithNibName:@"Cell2" bundle:nil] forCellReuseIdentifier:@"FIDCELL2"];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellID = @"";
if (indexPath.row % 2 == 0) {
cellID = @"FIDCELL";
} else {
cellID = @"FIDCELL2";
}
FriendTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
// Configure the cell...
cell.nameLabel.text = self.namesArray[indexPath.row];
cell.photoImageView.image = [UIImage imageNamed:self.photoNameArray[indexPath.row]];
return cell;
}
此代码部分完全有效。
好吧,首先,每个单元格都应该有其唯一标识符,所以如果第一个单元格有一个名为 "FIDCELL" 的标识符,第二个单元格应该有另一个名为 "FIDCELL2" 的标识符,然后删除您的 viewDidLoad 中的以下行:
[self.tableView registerNib:[UINib nibWithNibName:@"FriendTableViewCell" bundle:nil] forCellReuseIdentifier:@"FIDCELL"];
并将其添加到 cellForRowAtIndexPath
-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomTableViewCell * cell ;
if(condtion1)
{
cell = [tableView dequeueReusableCellWithIdentifier:firstCellIdentfier];
}
else if(condtion2)
{
cell = [tableView dequeueReusableCellWithIdentifier:secondCellIdentfier];
}
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:yourNibName owner:self options:nil];
if(condtion1)
{
//0 is the index of the first cell in the nib
cell = [nib objectAtIndex:0];
}
else if(condtion2)
{
//1 is the index of the second cell in the nib .
cell = [nib objectAtIndex:1] ;
}
}
//do other work
return cell ;
}
其中条件 1 和条件 2 是决定选择哪个单元格的条件。