自定义UItableView的实现以及组件不可见的问题及解决

       有时需要写一个自定义的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 tableViewinstantiate 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 Owneranf 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

         谢谢!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好,关于 Swift 封装 UITableView 和 UICollectionView 的问题,我可以给你一些基本的介绍。 首先,UITableView 和 UICollectionView 都是 iOS 平台中常用的视图控件,用于展示列表和网格等数据。在 Swift 中,我们可以通过继承 UITableView 和 UICollectionView 类来自定义我们自己的表格和网格视图。 下面是一个简单的示例代码,展示如何封装 UITableView: ```swift import UIKit class MyTableView: UITableView, UITableViewDataSource, UITableViewDelegate { var data: [String] = [] override init(frame: CGRect, style: UITableView.Style) { super.init(frame: frame, style: style) self.dataSource = self self.delegate = self // 注册单元格 self.register(UITableViewCell.self, forCellReuseIdentifier: "cell") } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } // UITableViewDataSource 协议方法 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return self.data.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) cell.textLabel?.text = self.data[indexPath.row] return cell } // UITableViewDelegate 协议方法 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { print("选中了第 \(indexPath.row) 行") } } ``` 在这个示例代码中,我们自定义了一个名为 `MyTableView` 的类,继承自 `UITableView`。我们在 `init` 方法中设置了数据源和代理,并注册了一个单元格类型。在 `UITableViewDataSource` 和 `UITableViewDelegate` 协议方法中,我们实现了表格的行数、单元格内容和选中事件的处理。 类似地,我们也可以使用类似的方式封装 UICollectionView。需要注意的是,UICollectionViewDelegate 和 UICollectionViewDataSource 两个协议方法和 UITableView 中的函数名和实现方式略有不同,需要根据实际情况来进行调整。 希望这个简单的示例代码可以对你有所帮助。如果你有其他关于 Swift 的问题,欢迎随时提出!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值