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

时间:2011-02-06 23:49:20

标签: ios uitableview

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

提前致谢

6 个答案:

答案 0 :(得分:15)

在iOS5中,您需要使用新的:

registerNib:forCellReuseIdentifier

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

答案 1 :(得分:10)

我个人认为,在reuseIdentifier方面,两个建议的教程都有一个很大的缺陷。如果您忘记在界面构建器中分配它或拼错它,您将在每次cellForRowAtIndexPath被调用时加载笔尖。

Jeff LaMarche在此blog post中写到了这一点以及如何解决这个问题。除了reuseIdentifier之外,他使用与Loading Custom Table-View Cells From Nib Files上的苹果文档相同的方法。

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

编辑:如果您定位的是iOS 5.0及更高版本,则需要坚持使用Duane Fields' answer

@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(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;
}

答案 2 :(得分:6)

显示了如何使用Xcode 4.2执行此操作的video tutorial。作者也写了blog post

答案 3 :(得分:1)

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

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

答案 4 :(得分:1)

`您可以在.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;

}

答案 5 :(得分:0)

您可以使用从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

相关问题