自定义UItableView的实现以及组件不可见的问题及解决
来源:互联网 发布:滴滴大数据研究中心 编辑:程序博客网 时间:2024/06/05 07:18
有时需要写一个自定义的UITableView,这里的自定义UITableView指的是自定义列表内容,通常就是实现一个UITableViewCell的子类,如MyTableViewCell。下面先来实现一个自定义组件。
STEP 1.
新建一个Empty Application,这里我就叫MyTableViewTest,勾选ARC选项。
STEP 2.
新建一个ViewController对象,注意不需要生成xib文件,这里我就叫MyViewController
STEP 3.
在AppDelegate.m中指定MyViewController为程序的rootViewController
//指定rootViewController对象 MyTableViewController *controller = [[MyTableViewController alloc]init]; self.window.rootViewController = controller;STEP 4.
新建自定义的列表项的xib文件,新建一个empty interface builder文件,就叫MyUITableViewCell,会自动生成为MyUITableViewCell.xib文件。拖动一个UITableViewCell组件进来,再将不同的组件(如UILabel,UIButton)拖动进该UITableViewCell中,注意将这个Table View Cell的Indentity inspector中的class属性设置为自定义的UITableViewCell,这里是MyUITableViewCel。详见截图:
STEP 5.
new一个UITableViewCell的子类,就叫MyUITableViewCell,这个类其实更像一个容器类。并对xib文件中的UILabel和UIButton进行连接。这里有一个需要注意的地方。就是MyUITableViewCell的File's owner是什么并不重要(比如可以是NSObject),只需要将Cell上的UIButton和UILable连接到Cell上即可。怎么连接呢?先在MyUITableViewCell.h中定义对应的property属性,如myLabel和myButton。然后右击UILable弹出一个黑色的框框,选择其中的Referencing Outlets下的New Referencing Outlet,拖到鼠标到左侧的组件MyUITableViewCell上,此时会自动弹出一个黑色的框框,选中myLabel即可。
详见下图:
这里必须讨论一下为什么将组件链接到MyUITabelViewCell上,而不是File's owner。其实刚开始我是直接把组件链接到File's owner上的,但是这样之后就直接报错了。于是就google,找了一段解释,可以看看
Normally you don't have to bother about the File's owner in that case, because when the tableView
instantiate the cell from the UINib
you provided / associated with the reuseIdentifier
, it will load all the top-level objects of the nib, and use only the first top-level object that is of classUITableViewCell
(or maybe juste the first top-level-object regardless of the class? but in general you only have your UITableViewCell
in your XIB anyway — without counting the File's Owner
and the First Responder
which are only "proxies").
In fact, when the tableView
try to dequeue a cell and don't find a reusable one, so create a new one for you, it uses the UINib
you provided quite like this:
NSArray* topLevelObjects = [self.cellNib instantiateWithOwner:nil options:0];cell = [topLevelObjects objectAtIndex:0];
(That's of course a simplified version just to show the principle, I don't know if it actually call only these exact lines, but it should be quite close)
So the File's Owner
is not used in this particular case, and you only need to put a simple customUITableViewCell
as the only top-level-object of your XIB file next to the existing File's Owner
anf First Responder
(that, again, are only "proxies" / "External Objects references" and won't be instantiated and won't be part of the top-level-objects returned byinstantiateWithOwner:options:
).
原文地址:http://stackoverflow.com/questions/12590471/uitableview-registernibforcellreuseidentifier
STEP 6.
因为没有为MyViewController生成xib文件,所以我们需要在MyViewController的代码中来手动添加组件,这里在viewDidLoad方法中添加。那么出于调用方便的考虑,可以在MyViewController.h中定义UITableView的属性。当然不要忘了实现UITableView对应的Delegate和Datasource协议。下面是 MyViewController.h:
//// MyTableViewController.h// MyTableViewTest//// Created by wly on 13-10-15.// Copyright (c) 2013年 wly. All rights reserved.//#import <UIKit/UIKit.h>@interface MyTableViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>@property (nonatomic,strong) IBOutlet UITableView *myTableView;@end
STEP 7.
在MyViewController.m中实现UITableView的代理方法,并将UITableView添加到self.view上即可。MyViewController.m:
//// MyTableViewController.m// MyTableViewTest//// Created by wly on 13-10-15.// Copyright (c) 2013年 wly. All rights reserved.//#import "MyTableViewController.h"#import "MyUITableViewCell.h"@interface MyTableViewController ()@end@implementation MyTableViewController@synthesize myTableView;- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self;}- (void)viewDidLoad{ [super viewDidLoad];// Do any additional setup after loading the view. //由于没有xib文件,即无法通过control建立组件和delegate或datasource之间的链接,这里就需要手动指定了 myTableView = [[UITableView alloc]init]; myTableView.delegate = self; myTableView.dataSource = self; //需要注意一个问题,手动添加组件,总是忘了设置tableview的frame属性,导致组件不可见 myTableView.frame = CGRectMake(0, 0, 320, 568); [self.view addSubview:myTableView];}- (void)didReceiveMemoryWarning{ [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 10;}-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 72.0;}-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *tag = @"cellTag"; //一个关键问题 //1.使用自定义的MyUITableViewCell,但是没有使用registerNib,显示空白行// MyUITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tag];// if(cell == nil) {// cell = [[MyUITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tag];// }// [cell.myButton setTitle:@"Let's go" forState:UIControlStateNormal];// [cell.myButton setBackgroundColor:[UIColor redColor]]; //2.使用默认的UITableViewCell,显示正确// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tag];// if(cell == nil) {// cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tag];// }// // [cell.textLabel setText:@"ABC"];// [cell.textLabel setTextColor:[UIColor redColor]];// [cell.imageView setImage:[UIImage imageNamed:@"default"]]; //3.使用自定义的MyUITableViewCell,显示正常 static BOOL nibsRegistered = NO; if(!nibsRegistered) { UINib *nib = [UINib nibWithNibName:@"MyUITableViewCell" bundle:nil]; [tableView registerNib:nib forCellReuseIdentifier:tag]; nibsRegistered = YES; } MyUITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tag]; [cell.myLabel setText:@"abc"]; return cell;}@end
程序运行结果:
O啦~~~,写了挺久的,终于好啦
转帖请保留出处:http://write.blog.csdn.net/postedit/12745165
工程下载地址:http://download.csdn.net/detail/u011638883/6404331
谢谢!!
- 自定义UItableView的实现以及组件不可见的问题及解决
- 设置View的可见以及不可见
- EditText作为密码框,点击可见不可见按钮,实现密码的可见和不可见
- 解决CCScrollView中菜单条目在不可见时仍响应点击事件的问题
- wpf问题~解决DataGrid列列头及可见性的绑定问题
- Android 3.2及以上系统 EditText光标不可见的问题
- vue component为什么官网的自定义组件不显示及各种自定义组件写法?
- 自定义的button在ios7下可见,在ios6下不可见的解决办法
- View的可见与不可见
- 56. 让不可见的更可见
- fragment可见和不可见的方法
- fragment可见和不可见的方法
- ios中为耗时动作添加indicatorview不可见问题及解决
- iOS瀑布流(UIScrollView或UITableView)和解决运行不流畅的问题
- 解决UITableView设置了偏移量但是不偏移的问题
- 关于自定义SeekBar写法以及seekbar不显示的问题
- 纯代码实现自定义UITableView的cell
- 解决iOS UITableView reloadData 时闪屏的问题
- [置顶] 我的六年程序之路
- 寻找二叉树中两个节点的最近的公共祖先
- 显示baseline的SQL的执行计划内容
- ASIHTTPRequest-流量控制
- SVN常用命令
- 自定义UItableView的实现以及组件不可见的问题及解决
- Hibernate查询详解
- ASIHTTPRequest-客户端证书支持
- RT5350调试总结
- ASIHTTPRequest-使用代理连接
- Unity3D研究院之Assetbundle的原理(六十一)
- iOS7或以后将不能使用MAC地址生成设备的唯一标识
- Altium Designer 10 导出文件(PDF,gerber,BOM)
- Android4.0 socket编程