[英]How to add a custom UITableViewCell to a xib file objective-c
I want to load my custom xib for tableviewcell in view controller.I can't load my xib in tableview我想在视图控制器中为 tableviewcell 加载我的自定义 xib。我无法在 tableview 中加载我的 xib
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:[CustomCell reuseIdentifier]];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
cell = _customCell;
_customCell = nil;
}
cell.topLabel.text = @"I am on top";
cell.bottomLabel.text = @"and I'm on the bottom";
return cell;
}
I cant't load my custom cell from xib into tableview.我无法将我的自定义单元格从 xib 加载到 tableview 中。 Please help me.
请帮帮我。
There are two options:有两种选择:
OPTION 1选项 1
First you can Register the Custom Cell in viewDidLoad首先,您可以在 viewDidLoad 中注册自定义单元格
UINib *nib = [UINib nibWithNibName:@"CustomCell" bundle:nil];
[tableView registerNib:nib forCellReuseIdentifier:@"cell"];
Then in cellForRowAtIndexPath of TableView DataSource methods:然后在 TableView DataSource 方法的cellForRowAtIndexPath中:
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:@"cell"];
cell.lblName.text = @"Tejas";
OPTION 2选项 2
If you are not registering the cell in viewDidLoad , you have to do the following things in cellForRowAtIndexPath of TableView DataSource methods:如果您没有在 viewDidLoad 中注册单元格,则必须在 TableView DataSource 方法的cellForRowAtIndexPath中执行以下操作:
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:@"cell"];
NSArray *nibs = [[NSBundle mainBundle]loadNibNamed:@"CustomCell" owner:self options:nil];
if(cell == nil)
cell = nibs[0];
cell.lblName.text = @"Tejas";
return cell;
Register the cell(XIB Cell) in the UITableView using the following method:使用以下方法在 UITableView 中注册单元格(XIB 单元格):
[self.tableView registerNib:[UINib nibWithName:nibName bundle:nil] forCellReuseIdentifier:@"CustomCell"];
Then you can dequeue the cell using the identifier you used for registering the cell.然后,您可以使用用于注册单元的标识符使单元出列。
You can do like this in Tableview cellforrowAtIndexPath methods,你可以在 Tableview cellforrowAtIndexPath 方法中这样做,
static NSString *CellIdentifier = @"CellData";
NSArray *arrData = [[NSBundle mainBundle]loadNibNamed:@"cellTaskDetail" owner:nil options:nil];
cellTaskDetail *cell = [[cellTaskDetail alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell = [arrData objectAtIndex:0];
return cell;
Its work fine...do it它的工作正常......做吧
NSString *simpleTableIdentifier = [NSString stringWithFormat:@"%d_%d",indexPath.section,indexPath.row];
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
cell = [nibArray objectAtIndex:0];
}
cell.topLabel.text = @"I am on top";
cell.bottomLabel.text = @"and I'm on the bottom";
return cell;
Using loadNibNamed
simple solution使用
loadNibNamed
简单解决方案
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"CellIdentifier";
YourCustomeCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{ cell = [[[NSBundle mainBundle] loadNibNamed:@"YourCustomeCell" owner:self options:nil] objectAtIndex:0];
}
// do code with cell here
return cell;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.