如何获得在IB创build的自定义UITableViewCell类加载到UITableView?

我是新来的,所以请忍受我:

我在IB中有一个.xib文件,里面包含一个embedded了UITableView的UIScrollView。 我想加载自定义UITableViewCells到UITableView。 我在IB中创build了四个/五个自定义单元格,但无法正确加载第一个单元格。

这里是来自IB的相关截图:

的.xib自定义单元格“itemCell”

我已经在IB创build的自定义UITableViewCell的类设置为我创build称为“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 

在filecontroller.m,这是根控制器.xib的实现文件,我创build一个新的单元格基于用户按下一个button(将其添加到数组),然后重新加载tableview。 我没有得到任何的错误,但我得到一个正常的细胞加载而不是我在IB创build的。 这里是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开始,您现在可以使用以下函数从一个笔尖加载一个UITableViewCell以便重复使用笔记本电脑。

– (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)标识符

  1. 首先确保你创build一个只包含UITableViewCell的IB文件
  2. 在Identify检查器下:将UITableViewCell的类设置为“itemCell” 在这里输入图像说明
  3. 在属性检查器下:将UITableViewCell的标识符设置为“itemCellIdentifier” 在这里输入图像说明
  4. 在viewDidLoad中:放置以下代码,并将itemCellNibNamereplace为包含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; } 

另外:你不想在UIScrollView中包含UITableView,UIScrollView已经内置在UITableView中,它允许正确的细胞排队。

另外:我build议你在开始从一个数组中抽取数据之前,首先得到这个简单的骨骼。

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

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

在你的

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { //This identifier string needs to be set in cell of the TableViewController in the storyboard static NSString *CellIdentifier = @"Cell"; // Resuse the cell SomeCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; // If there are no cell to be reused we create a new one if(cell == nil) { cell = [SomeCustomCell new]; } // Configure the cell... NSString *someString = [yourArrayWithStringsOrWhatEver objectAtIndex:indexPath.row]; // Setting the some property of your custom cell [cell.yourSpecificCellProperty someString]; [cell.yourOtherSpecificCellProperty setText:@"What Ever your want!"]; } 

你的单元格的属性可以是任何你已经连接到你的自定义单元格的界面生成器。

希望能帮助到你!

那么有一些事情是不正确的:

  • 类名以大写字母itemCell – > ItemCell
  • 你应该重用这个单元格。 您要这样做的方式,您将为每个索引path创build一个单独的单元格。
  • items数组应该只包含您的表的数据源,而不是您的单元格
  • 而不是调用重载数据,你可以只添加一行到表,也可以animation

尝试像这样:

 - (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 { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (!cell) { cell = [[NSBundle mainBundle] loadNibNamed:@"itemCell" owner:self options:nil].lastObject; cell.reuseIdentifier = CellIdentifier; } // Do any additional setup to the cell return cell; } - (IBAction)addItem:(id)sender { [items addObject:[NSNumber numberWithInt:items.count]]; // Here comes the data source [self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:items.count - 1 inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic]; } 

PS:我没有IDE编写它,所以可能会有一些错误