1、File----New File---Cocoa Touch Classes---Object-C Class
创建一个 从 UITabelViewCell继承的类 UICustomTableViewCell
2、File---NewFile----User Interface-----Empty XIB或者File---New File----User Interface-----View XIB
创建一个空的 UICustomTableViewCell.xib 文件,记住,XIB的名称一定要跟 签名的类的名称一致,也就是一模一样。
如果是View的XIB类型,那么创建的XIB里面的已经存在的那个UIView,需要自己选择Size为Freeform,然后自己设置大小。
如果是空的XIB文件,所有往文件列表框中拖入一个 UIView的控件,然后将这个UIView的类名在属性器里面从UIView
改为UICustomTableViewCell。
3、这样,就可以往这个新添加的View里面添加我们自己的个性化控件了,这个View就是我们的Cell的模板了。这个过程跟普通的XIB一样,没有什么特别的。
那么如何在代码中使用这个UICustomTableViewCell呢?
代码如下:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UICustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UICustomTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"UICustomTableViewCell" owner:self options:nil];
//这时myCell对象已经通过自定义xib文件生成了
if ([nib count]>0) {
cell = (UICustomTableViewCell *)[nib objectAtIndex:0];
//加判断看是否成功实例化该cell,成功的话赋给cell用来返回。
}
//若是需要重用,需要写上以下两句代码
UINib * n= [UINib nibWithNibName:@"UICustomTableViewCell" bundle:[NSBundle mainBundle]];
[self.tableView registerNib:n forCellReuseIdentifier:CellIdentifier];
}
//添加内容
return cell;
}