UITableViewCell的用xib文件重新设计

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中取到进行判断再予以区别执行不同的事件。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值