如何获取在IB中创建的自定义UITableViewCell类加载到UITableView? [英] How to get a custom UITableViewCell class created in IB to load into a UITableView?
问题描述
我是新手,所以请耐心等待:
I am new to this so please bear with me:
我在IB中有一个包含UIScrollView的.xib,其中嵌入了UITableView。我想将自定义UITableViewCells加载到UITableView中。我在IB中创建了四个/五个自定义单元格,但无法正确加载第一个。
I have a .xib in IB containing a UIScrollView that has a UITableView embedded in it. I would like to load custom UITableViewCells into the UITableView. I have four/five custom cells created in IB but cannot get the first to load correctly.
以下是来自IB的相关屏幕截图:
Here are relevant screenshots from IB:
我已将在IB中创建的自定义UITableViewCell的类设置为UITableView我创建的类称为itemCell。另外,我已经将三个文本字段的出口和代表连接到itemCell。
I have set the class for the custom UITableViewCell created in IB to the UITableView class I created called "itemCell". Additionally, I have connected the three text fields outlets and delegates to "itemCell".
这是itemCell.h:
Here is 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:
Here is 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中,它是rootcontroller .xib的实现文件,我根据用户按下按钮创建一个新单元格(将其添加到数组中),然后重新加载tableview。我没有得到任何错误,但我得到的是正常的单元格而不是我在IB中创建的单元格。这是rootcontroller.m。任何帮助表示赞赏。
In filecontroller.m, which is the rootcontroller .xib's implementation file I create a new cell based on the user pressing a button (add it to an array) and then reload the tableview. I am getting no errors but I am getting a normal cell loaded in rather than what I have created in IB. Here is rootcontroller.m. Any help is appreciated.
- (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使用以下函数重写单元格。
As of iOS 5 you can now load a UITableViewCell from a nib for cell reuse with the following function.
- (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier
- 首先确保创建仅包含UITableViewCell的IB文件
- 在识别检查器下:将UITableViewCell的类设置为itemCell
- 在属性检查器下:将UITableViewCell的标识符设置为itemCellIdentifier
-
在viewDidLoad中:放置以下代码,并将itemCellNibName替换为包含UITableViewCell的nib的名称。
- First make sure you create an IB file that contains only the UITableViewCell
- Under Identify inspector: Set the class of the UITableViewCell to "itemCell"
- Under attributes inspector: Set the identifier of the UITableViewCell to "itemCellIdentifier"
In viewDidLoad: place the following code, and replace itemCellNibName with the name of the nib that contains the UITableViewCell.
NSString *myIdentifier = @"itemCellIdentifier";
[self.tableView registerNib:[UINib nibWithNibName:@"itemCellNibName" bundle:nil] forCellReuseIdentifier:myIdentifier];
在cellForRowAtIndexPath中:放置以下代码,您现在可以访问您设置的IBOutlet属性itemCell,并在笔尖中连接。
In cellForRowAtIndexPath: place the following code, you can now access your IBOutlet properties you set on the itemCell, and connected in the nib.
-(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中,允许正确的单元格排队。
Also: You do not want to include the UITableView inside a UIScrollView, a UIScrollView is already built into the UITableView, which allows for proper cell queing.
另外:我建议你刚刚接受在开始从数组中提取数据之前,首先要做的就是这个。
Also: I suggest you just get the bare bones of this going first, before you start pulling data from an array.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 10;
}
另外:正如其他人提到的那样,数组包含将填充的数据UITableViewCells,而不是实际的单元格。
Also: As someone else had mentioned, the array contains the data that will populate the UITableViewCells, not the actual cells themselves.
这篇关于如何获取在IB中创建的自定义UITableViewCell类加载到UITableView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!