如何将 IB 中创建的自定义 UITableViewCell 类加载到 UITableView 中?

2023-11-22

我对此很陌生,所以请耐心等待:

我在 IB 中有一个 .xib,其中包含一个 UIScrollView,其中嵌入了一个 UITableView。我想将自定义 UITableViewCells 加载到 UITableView 中。我在 IB 中创建了四/五个自定义单元格,但无法正确加载第一个单元格。

以下是IB的相关截图:

.xib custom cell "itemCell"

我已将 IB 中创建的自定义 UITableViewCell 的类设置为我创建的名为“itemCell”的 UITableView 类。此外,我已将三个文本字段出口和委托连接到“itemCell”。

这是 itemCell.h:

#import <UIKit/UIKit.h>

@interface itemCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UITextField *quantityTextField;
@property (weak, nonatomic) IBOutlet UITextField *itemTextField;
@property (weak, nonatomic) IBOutlet UITextField *priceTextField;

@end

这是 itemCell.m:

#import "itemCell.h"

@implementation itemCell
@synthesize quantityTextField,itemTextField,priceTextField;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code

    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

在 rootcontroller .xib 的实现文件 filecontroller.m 中,我根据用户按下的按钮创建一个新单元格(将其添加到数组中),然后重新加载表格视图。我没有收到任何错误,但我加载了一个正常的单元格,而不是我在 IB 中创建的单元格。这是rootcontroller.m。任何帮助表示赞赏。

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    scrollView =(UIScrollView *)self.view;
    items = [[NSMutableArray alloc] init];
}

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


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        return [items objectAtIndex:[indexPath row]];
}

- (UITableViewCell *)initiateNewCell{
    itemCell *cell = [[itemCell alloc] init];
    cell.accessoryType = UITableViewCellAccessoryNone;
    cell.selectionStyle = UITableViewCellSelectionStyleBlue;
    return cell;
}

- (IBAction)addItem:(id)sender {
    //creates the new cell to be added
    [items addObject:[self initiateNewCell]];
    [self.tableView reloadData];

从 iOS 5 开始,您现在可以使用以下函数从 nib 加载 UITableViewCell 以便重复使用单元格。

- (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier

  1. 首先确保您创建一个仅包含 UITableViewCell 的 IB 文件
  2. Under Identify inspector: Set the class of the UITableViewCell to "itemCell" enter image description here
  3. Under attributes inspector: Set the identifier of the UITableViewCell to "itemCellIdentifier" enter image description here
  4. 在 viewDidLoad: 中放置以下代码,并将 itemCellNibName 替换为包含 UITableViewCell 的 nib 的名称。

    NSString *myIdentifier = @"itemCellIdentifier";
    [self.tableView registerNib:[UINib nibWithNibName:@"itemCellNibName" bundle:nil] forCellReuseIdentifier:myIdentifier];
    
  5. 在 cellForRowAtIndexPath: 中放置以下代码,您现在可以访问您在 itemCell 上设置并在笔尖中连接的 IBOutlet 属性。

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
    {
         static NSString *myIdentifier = @"itemCellIdentifier";
    
         itemCell *cell = (itemCell *)[tableView dequeueReusableCellWithIdentifier:myIdentifier];
         cell.textLabel.text = [NSString stringWithFormat:@"Test Row:%i!",indexPath.row];
         return cell;
    }
    

另外:您不想将 UITableView 包含在 UIScrollView 中,UIScrollView 已内置到 UITableView 中,这允许正确的单元格排队。

另外:我建议您在开始从数组中提取数据之前先了解一下基本情况。

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
         return 10;
    }

另外:正如其他人提到的,该数组包含将填充 UITableViewCells 的数据,而不是实际的单元格本身。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何将 IB 中创建的自定义 UITableViewCell 类加载到 UITableView 中? 的相关文章

随机推荐