如何从xib创建自定义tableViewCell [英] how to create custom tableViewCell from xib
问题描述
我想创建一个自定义的TableViewCell,我希望UITextField具有编辑功能。
所以我用xib创建了新类。添加TableViewCell元素。在它上面拖动UITextField。在我班级添加了插座并将它们连接在一起。在我的TableView方法cellForRowAtIndexPath中我创建自定义单元格,但它们不是我的自定义单元格 - 它们只是常用的单元格。我该如何解决这个问题,为什么会这样? thanx!
I want to create a custom TableViewCell on which I want to have UITextField with editing possibility. So I created new class with xib. Add TableViewCell element. Drag on it UITextField. Added outlets in my class and connect them all together. In my TableView method cellForRowAtIndexPath I create my custom cells, BUT they are not my custom cells - they are just usual cells. How can I fix this problem, and why it is? thanx!
// EditCell。 h
//EditCell. h
#import <UIKit/UIKit.h>
@interface EditCell : UITableViewCell
{
IBOutlet UITextField *editRow;
}
@property (nonatomic, retain) IBOutlet UITextField *editRow;
@end
// EditCell.m
//EditCell.m
#import "EditCell.h"
@implementation EditCell
@synthesize editRow;
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidUnload
{
// Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
// For example: self.myOutlet = nil;
self.editRow = nil;
}
@end
//在我的代码中
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"EditCell";
EditCell *cell = (EditCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[EditCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier] autorelease];
}
cell.editRow.text = @"some text to test";
return cell;
}
推荐答案
不要使用UITableViewCell的初始化程序,但是从你的笔尖加载单元格:
Do not use UITableViewCell's initializer, but make the cell load from your nib:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"EditCell";
EditCell *cell = (EditCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"YourNibNameHere" owner:self options:nil];
cell = (EditCell *)[nib objectAtIndex:0];
}
cell.editRow.text = @"some text to test";
return cell;
}
当然,您需要指定正确的笔尖名称。
Of course, you need to specify the correct nib name.
这篇关于如何从xib创建自定义tableViewCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!