跟着书上的范例做完了一般的table view,然后做做自定义表格,也遇到一些问题,最后终于解决了,记录下怎么弄出来的吧。
知识点:
1.自定义表格cell的格式。
2.自定义表格cell的view类。
3.使用自定义的view来呈现数据。
一、自定义表格cell的格式
新建xib文件,拖table view cell,label,image view到画板上,如图:
二、自定义表格cell的view类
新建objective-c class,继承UITableViewCell类,见代码:
//
// SimpleTableViewCellController.h
// SimpleTableStoryBoradTest
//
// Created by wong linwei on 12-9-8.
// Copyright (c) 2012年 P&T. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface SimpleTableViewCell : UITableViewCell {
UIImageView *cellImage;
UILabel *cellName;
UILabel *cellTime;
}
@property (strong, nonatomic) IBOutlet UIImageView *cellImage;
@property (strong, nonatomic) IBOutlet UILabel *cellName;
@property (strong, nonatomic) IBOutlet UILabel *cellTime;
@end
//
// SimpleTableViewCellController.m
// SimpleTableStoryBoradTest
//
// Created by wong linwei on 12-9-8.
// Copyright (c) 2012年 P&T. All rights reserved.
//
#import "SimpleTableViewCell.h"
@implementation SimpleTableViewCell
@synthesize cellImage;
@synthesize cellName;
@synthesize cellTime;
- (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
三、使用自定义view类
在有table view的view controller类中实现UITableViewDelegate,UITableViewDataSource两个协议。
//
// PNTViewController.h
// SimpleTableStoryBoradTest
//
// Created by wong linwei on 12-9-5.
// Copyright (c) 2012年 P&T. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface PNTViewController : UIViewController <UITableViewDelegate,UITableViewDataSource>
{
NSArray *tableData;
}
@end
//
// PNTViewController.m
// SimpleTableStoryBoradTest
//
// Created by wong linwei on 12-9-5.
// Copyright (c) 2012年 P&T. All rights reserved.
//
#import "PNTViewController.h"
#import "SimpleTableViewCell.h"
@implementation PNTViewController
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
tableData = [NSArray arrayWithObjects:@"first",@"second",@"third",@"forth",@"fifth",@"sixth", nil];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"SimpleTableViewCell";
SimpleTableViewCell *cell = (SimpleTableViewCell*)[tableView dequeueReusableCellWithIdentifier:identifier];//复用cell
if (cell == nil) {
NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableViewCell" owner:self options:nil];//加载自定义cell的xib文件
cell = [array objectAtIndex:0];
}
cell.cellName.text = [tableData objectAtIndex:indexPath.row];
cell.cellTime.text = [NSString stringWithFormat:@"%@",[NSDate date]];
cell.cellImage.image = [UIImage imageNamed:@"titlepic.png"];
return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [tableData count];//返回有多少项数据要显示
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 80;//此处返回cell的高度
}
@end
看下最后的结果吧:
出现的问题是:
1.运行是出现空白的表格,这种情况的发生,一是datadelegate和datasource两个IBOutlet没有连接上,二是因为给cell取的identifier不要跟复用的那个identifier一样。
2.在加载自定义cell布局的时候,发生crash现象,因为cell取得identifier跟复用的identifier一样了。
记得如果出不来,就去模拟器里面停止应用并且删除以后再run即可。