自定义创建TableViewCell

通过继承UITableViewCell来自定义cell

1、创建一个空的项目、命名:

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

3、设置AppDelegate.m中window的根控制器为刚刚创建的TableViewController:

- (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];

returnYES;

}

4、创建自定义的UITableViewCell:

5、创建自定义cell的xib 拖放需要的控件

选择User Interface。

创建空的xib。

拖入Cell控件。

完成自定义的cell控件。

设置cell控件的Identfier。

绑定Cell类并且将控件的输出口关联到TableViewCell.h文件中。

6、对TableViewController类编码,在委托方法中设置自定义的Cell:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

//指定cellIdentifier为自定义的cell

staticNSString *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"];

returncell;

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容