ios 使用 xib 文件创建自定义 UITableViewCell 的步骤

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4917027/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:49:00  来源:igfitidea点击:

ios steps to create custom UITableViewCell with xib file

iosuitableview

提问by ghiboz

I need to create my own UITableViewCell using the xib file, to draw the graphic interface... what are the right steps to create my new class and to use into my UITableView?

我需要使用 xib 文件创建我自己的 UITableViewCell 来绘制图形界面......创建我的新类并用于我的 UITableView 的正确步骤是什么?

thanks in advance

提前致谢

回答by Duane Fields

In iOS5 you'll want to use the new:

在 iOS5 中,你会想要使用新的:

registerNib:forCellReuseIdentifier:

registerNib:forCellReuseIdentifier

Which basically does the same thing...

基本上做同样的事情......

回答by christoph

Personally I think that both suggested tutorials have a big flaw when it comes to reuseIdentifier. If you forget to assign it in interface builder or misspell it, you will load the nib each and every time cellForRowAtIndexPathgets called.

就个人而言,我认为这两个建议的教程在涉及reuseIdentifier. 如果您忘记在界面构建器中分配它或拼写错误,您将在每次cellForRowAtIndexPath调用时加载笔尖。

Jeff LaMarche writes about this and how to fix it in this blog post. Apart from reuseIdentifierhe uses the same approach as in the apple documentation on Loading Custom Table-View Cells From Nib Files.

Jeff LaMarche 在这篇博文中写到了这个问题以及如何修复它。除了reuseIdentifier他使用与从 Nib 文件加载自定义表视图单元格的苹果文档中相同的方法。

After having read all these articles I came up with following code:

阅读完所有这些文章后,我想出了以下代码:

Edit: If you are targeting iOS 5.0 and higher you'll want to stick with Duane Fields' answer

编辑:如果你的目标是 iOS 5.0 及更高版本,你会想要坚持Duane Fields 的回答

@interface CustomCellWithXib : UITableViewCell

+ (NSString *)reuseIdentifier;
- (id)initWithOwner:(id)owner;

@end

@implementation CustomCellWithXib

+ (UINib*)nib
{
    // singleton implementation to get a UINib object
    static dispatch_once_t pred = 0;
    __strong static UINib* _sharedNibObject = nil;
    dispatch_once(&pred, ^{
        _sharedNibObject = [UINib nibWithNibName:NSStringFromClass([self class]) bundle:nil];
    });
    return _sharedNibObject;
}

- (NSString *)reuseIdentifier
{
    return [[self class] reuseIdentifier];
}

+ (NSString *)reuseIdentifier
{
    // return any identifier you like, in this case the class name
    return NSStringFromClass([self class]);
}

- (id)initWithOwner:(id)owner
{
    return [[[[self class] nib] instantiateWithOwner:owner options:nil] objectAtIndex:0];
}

@end

UINib (available in iOS 4.0 and later) is used here as a singleton, because although the reuseIdentifieris used, the cell still gets re-initialized about 10 times or so. Now cellForRowAtIndexPathlooks like this:

UINib(在 iOS 4.0 及更高版本中可用)在这里用作单例,因为尽管reuseIdentifier使用了,但单元格仍然会重新初始化大约 10 次左右。现在cellForRowAtIndexPath看起来像这样:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CustomCellWithXib *cell = [tableView dequeueReusableCellWithIdentifier:[CustomCellWithXib reuseIdentifier]];
    if (cell == nil) {
        cell = [[CustomCellWithXib alloc] initWithOwner:self];
    }

    // do additional cell configuration

    return cell;
}

回答by Rich Apodaca

A video tutorialshowing how to do this with Xcode 4.2 has been made. The author has written a blog postas well.

一个视频教程演示如何在Xcode 4.2做到这一点已经取得进展。作者也写了一篇博文

回答by kraftydevil

This tutorial takes you through the whole modern iOS 5 solution, from the process of creating the cell's xib and class files all the way to the finish:

本教程将带您了解整个现代 iOS 5 解决方案,从创建单元格的 xib 和类文件的过程一直到完成:

http://mrmaksimize.com/ios/Custom-UITableViewCell-With-NIB/

http://mrmaksimize.com/ios/Custom-UITableViewCell-With-NIB/

回答by Mohammad Kamran Usmani

`You can create custom cells in table view with the help of .xib file. First setup the table view in your view controller, create a new xib file with its class and use it in table view.

`您可以借助 .xib 文件在表格视图中创建自定义单元格。首先在您的视图控制器中设置表视图,使用其类创建一个新的 xib 文件并在表视图中使用它。

- (IBAction)moveToSubCategory:(id)sender;
@property (strong, nonatomic) IBOutlet UILabel *foodCategoryLabel;
@property (strong, nonatomic) IBOutlet UIImageView *cellBg;



-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [foodCatArray count];
}



-(UITableViewCell *)tableView:(UITableView *)tableView     cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *simpleTableIdentifier = @"ExampleCell";
        ExampleCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
       if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ExampleCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
     }   
    [cell setTag:indexPath.row];
    cell.cellBg.image=[UIImage imageNamed:[photoArray objectAtIndex:indexPath.row]];
    cell.foodCategoryLabel.text=[foodCatArray objectAtIndex:indexPath.row];
    return cell;

}

回答by Bharat

You can create CustomCell class with XIB that is inherited from UITableViewCell. We will just add category in tableview class .m file in following way. I think this is the easiest method which an be applied for custom cell creation.

您可以使用继承自 UITableViewCell 的 XIB 创建 CustomCell 类。我们将通过以下方式在 tableview 类 .m 文件中添加类别。我认为这是用于创建自定义单元格的最简单方法。

    @interface UITableViewCell(NIB)
    @property(nonatomic,readwrite,copy) NSString *reuseIdentifier;
    @end
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return 30;
    }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    static NSString *identifier=@"cell";
        CustomCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
        if(cell==nil)
        {
             NSLog(@"New Cell");
            NSArray *nib=[[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
            cell=[nib objectAtIndex:0];
            cell.reuseIdentifier=identifier;

        }else{
            NSLog(@"Reuse Cell");
        }
        cell.lbltitle.text=[NSString stringWithFormat:@"Level %d",indexPath.row];
        id num=[_arrslidervalues objectAtIndex:indexPath.row];
        cell.slider.value=[num floatValue];
        return cell;
    }
    @end