UITableViewCell的用xib文件重新设计
来源:互联网 发布:java实现快速排序 编辑:程序博客网 时间:2024/06/11 16:49
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中取到进行判断再予以区别执行不同的事件。
- UITableViewCell的用xib文件重新设计
- 用.xib文件自定义UITableViewCell
- Xib文件创建UITableViewCell
- Xib文件创建UITableViewCell
- 使用xib文件自定义UItableViewCell
- 使用xib文件创建UITableViewCell
- 使用XIB文件来定制你的UITableViewCell
- storyboard中用xib文件创建定制的UITableViewCell对象
- storyboard中用xib文件创建定制的UITableViewCell对象 .
- storyboard中用xib文件创建定制的UITableViewCell对象
- 如何使用xib自定义UITableViewCell xib文件的创建和添加控件
- Xib设计UITableViewCell然后动态加载
- Xib设计UITableViewCell然后动态加载
- 用xib自定义UITableViewCell的注意事项——重用问题
- 用xib自定义UITableViewCell的注意事项——重用问题 -
- ios学习笔记:用xib自定义UITableViewCell的注意事项
- 用xib自定义UITableViewCell的注意事项——重用问题
- 用xib自定义UITableViewCell的注意事项——重用问题
- sql中视图
- $smarty.now
- Linux内核代码风格 CodingStyle
- 多线程(二)——————面试题详解
- == The History of Cryptography/ Algorithms/ conceptual modeling ==
- UITableViewCell的用xib文件重新设计
- java的环境变量配置
- yum命令详解
- 是否可以用sql语句实现分数的分段人数统计解决方法
- 技能模块二代机
- == 理论计算机科学学习网站 ==
- C#委托
- 常用sql语句
- Javascript 出现”未结束的字符串常量”错误