简体   繁体   English

如何将自定义 UITableViewCell 添加到 xib 文件中

[英]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.

相关问题 如何使用objective-C验证由UITableViewCell在自定义文件中创建的多个自定义UItextfield值 - How to validate a multiple custom UItextfield value which is created by UITableViewCell in custom file using objective-C 在.xib的底部添加边框(objective-c) - Add border to bottom of .xib (objective-c) Objective-C在另一个XiB中使用自定义XiB - Objective-C Using custom XiB in another XiB 如何在 Objective-C Controller 中以编程方式加载用 swift 编写的自定义 XIB 视图 - How to load a custom XIB view written in swift programatically in an Objective-C Controller iOS Objective-C:如何在Xcode项目中同时具有启动xib文件和静态启动图像? - iOS Objective-C: How to have both Launch xib file and static launch images in Xcode project? Objective-C:如何将字符串传递到.xib文件并显示MapView? - Objective-C: How can I pass string to a .xib file and display a MapView? Objective-C:应用范围内完全可重用的自定义UITableViewCell - Objective-C: App-wide fully reusable custom UITableViewCell 如何在Objective-c的自定义UITableViewCell中更改UIButon文本属性 - How do you change the UIButon text attribute in a custom UITableViewCell in Objective-c 如何借助AutoLayout在自定义UITableviewCell中添加自定义UIView(xib)? - How to add custom UIView(xib) in custom UITableviewCell with the help of AutoLayout? .xib文件中的自定义UITableViewCell - Custom UITableViewCell from a .xib file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM