通过继承uitableviewcell来自定义cell
1、创建一个空的项目、命名:


2、创建一个uitableviewcontroller 并且同时创建xib:

3、设置appdelegate.m中window的根控制器为刚刚创建的tableviewcontroller:
?| 1 2 3 4 5 6 7 8 9 |
- (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions
{
self.window = [[uiwindow alloc] initwithframe:[[uiscreen mainscreen] bounds]];
tableviewcontroller *tableviewcontroller = [[[tableviewcontroller alloc] init] autorelease]; //自动释放
//设置根控制器
self.window.rootviewcontroller = tableviewcontroller;
[self.window makekeyandvisible];
return yes;
}
|
4、创建自定义的uitableviewcell:

5、创建自定义cell的xib 拖放需要的控件
选择user interface。
创建空的xib。
拖入cell控件。
完成自定义的cell控件。
设置cell控件的identfier。
绑定cell类并且将控件的输出口关联到tableviewcell.h文件中。
6、对tableviewcontroller类编码,在委托方法中设置自定义的cell:
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
#import "tableviewcontroller.h"
#import "tableviewcell.h"
@interface tableviewcontroller (){
nsmutablearray *tabledata; //表格数据
}
@end
@implementation tableviewcontroller
- (id)initwithstyle:(uitableviewstyle)style
{
self = [super initwithstyle:style];
if (self) {
// custom initialization
}
return self;
}
- (void)viewdidload
{
[super viewdidload];
//初始化表格数据
tabledata = [[nsmutablearray alloc] init];
for (int i = 0; i< 10; i++) {
[tabledata addobject:[nsstring stringwithformat:@"mycelldemon%i",i]];
}
//设置row的高度为自定义cell的高度
self.tableview.rowheight = 90;
}
- (void)didreceivememorywarning
{
[super didreceivememorywarning];
}
#pragma mark - table view data source
- (nsinteger)numberofsectionsintableview:(uitableview *)tableview
{
#warning potentially incomplete method implementation.
return 1;
}
- (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section
{
#warning incomplete method implementation.
return [tabledata count];
}
- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath
{
//指定cellidentifier为自定义的cell
static nsstring *cellidentifier = @"tableviewcell";
//自定义cell类
tableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier];
if (cell == nil) {
//通过xib的名称加载自定义的cell
cell = [[[nsbundle mainbundle] loadnibnamed:@"tableviewcell" owner:self options:nil] lastobject];
}
//添加测试数据
cell.titlelabel.text = [tabledata objectatindex:indexpath.row];
cell.content.text = @"这是一些测试数据";
//测试图片
cell.iamge.image = [uiimage imagenamed:@"testimage.jpg"];
return cell;
}
#pragma mark - table view delegate
- (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath
{
}
@end
|
最终效果:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://blog.csdn.net/swingpyzf/article/details/10012779








发表评论
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。