iPhone开发秘笈书上有这类的例子的,在github上有实例,可以下载下来看下
网站地址:https://github.com/erica/iphone-3.0-cookbook-
在c11-table的07文件夹。
在xcode中新建一个singleview项目,删除原先的viewcontroller,新建文件cellViewController基于UitableviewController类,
cellViewController.h:
#import <UIKit/UIKit.h>
@interface cellViewController : UITableViewController
@end
cellViewController.m:
#import "cellViewController.h"
#import "CustomCell.h"
@interface cellViewController ()
@property(nonatomic,retain)NSArray* array;
@end
@implementation cellViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
_array=@[@"11",@"22"];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return _array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil] lastObject];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
}
cell.label.text=[_array objectAtIndex:indexPath.row];
// Configure the cell...
return (UITableViewCell*)cell;
}
从上面.m文件中可以看到我们import进来了一个CustomCell的类,再cellForRowAtIndexPat事件中cell实例对象就是由CustomCell申明的。所以我们需要新建一个CustomCell的类来对原先的uitableviewcell利用xib来进行设计。
我们先新建file基于uitableviewcell类的.h和.m文件。再新建xib文件选新建file下的iOS-User Interface选empty新建一个iOS Interface新建的三个文件都以CustomCell命名,再选中CustomCell.xib文件拖入一个uitableviewcell进入(一下都在xib中操作),选中cell将其Custom Class中的Class设置成CustomCell。再在这个cell中拖入一个label和一个button。在xib中操作到这。
再在CustomCell中新建对应的label和button和button对应的事件
CustomCell.h:
#import <UIKit/UIKit.h>
@interface CustomCell : UITableViewCell
@property(nonatomic,strong)IBOutlet UILabel *label;
@property(nonatomic,strong)IBOutlet UIButton *button;
-(IBAction)buttonClick:(id)sender;
@end
CustomCell.m:
#import "CustomCell.h"
@implementation CustomCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
-(IBAction)buttonClick:(id)sender
{
NSLog(@"%@",_label.text);
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
再回到CustomCell.xib中进行连接操作,将对应的label和button与CustomCell.h中的连接好还包括button事件的连接。
最后运行tableview中的cell就是xib中设置的样式进行显示,点击button按钮就会nslog出对应的label值。如果你需要对应不同的cell执行不同的事件那么你就可以将cellid之类的赋值给一个label再在button中取到进行判断再予以区别执行不同的事件。