是否有人使用TableViewController而没有子类化? [英] Are there Anyone that use TableViewController without subclassing?

查看:85
本文介绍了是否有人使用TableViewController而没有子类化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇.在IB中,我们可以放置一个tableviewcontroller.但是,据我所知,我们总是将那个tableview控制器子类化,对吗?这样我们就可以实现委托等.

I am just curious. In IB, we can put a tableviewcontroller. However, as far as I know, we always subclass that tableview controller right? That way we can implement delegate, etc.

但是,对于某些默认"行为,iPhone似乎希望原样使用tableviewcontroller.否则,为什么IB让我们像这样放置tableViewController?

However, it seems that for some "default" behavior, IPhone intended tableviewcontroller to be used as is. Otherwise, why would IB let us put tableViewController like that?

有人在不使用子类的情况下使用tableViewController的示例代码吗?

Are there any sample code where people use tableViewController without subclassing?

它们在哪里实现诸如绘制哪些单元格之类的东西?

Where does they implement things like what cells to draw, etc. then?

我想这个问题的正确答案是,使用UITableViewController而不使用子类只是荒谬的.没有人这样做.如果我错了,请纠正我.我很好奇.

I guess the right answer of the question is that it's simply ridiculous to use a UITableViewController without sub classing. No body is doing it. Please correct me if I am wrong. I am just curious.

推荐答案

无论您使用的是UITableViewController还是UIViewController的子类,都需要设置表将要显示的数据,否则,这意味着什么?空白表?为此,您必须继承并实现一些方法.将委托和数据源保留在同一控制器中也是一个好主意,除非复杂度确实要求使用不同的类.

Whether you use a subclass of UITableViewController or UIViewController you need to set the data your table is going to display, otherwise, what's the point of a blank table? To achieve that you have to subclass and implement some methods. It's also a good idea to keep the delegate and the datasource in the same controller, unless the complexity really asks for different classes.

话虽如此,我总是将自己的表控制器创建为UIViewController的子类,并自己实现表控制器方法,因为它为您提供了更大的灵活性.马特·加拉格尔(Matt Gallagher)关于如何以及为什么发表了几篇文章.请参见 UITableView的构造,绘制和管理(重新访问).

That being said, I always create my own table controllers as a subclass of UIViewController and implement the table controllers methods myself, because it gives you more flexibility. Matt Gallagher has several posts on how and why. See UITableView construction, drawing and management (revisited).

如果想尝试一下,请使用XIB创建UIViewController的子类,并添加以下示例代码:

If you want to give it a try, create a subclass of UIViewController with a XIB and add the following sample code:

// interface
#import <UIKit/UIKit.h>
@interface SettingsVC : UIViewController <UITableViewDelegate, UITableViewDataSource> 
@property (nonatomic, retain) IBOutlet UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *array;
@end

// implementation
@synthesize tableView = _tableView;
@synthesize array = _array;
# pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.array count];
}
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    int row = [indexPath row];
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    cell.textLabel.text = [self.array objectAtIndex:row];
    return cell;
}

然后将UITableView对象添加到XIB,将控制器的tableView链接到UITableView对象,并将UITableView的委托和数据源链接到控制器.

Then add a UITableView object to the XIB, link the tableView of the controller to the UITableView object, and link the delegate and datasource of the UITableView to the controller.

这篇关于是否有人使用TableViewController而没有子类化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆