我怎样才能将UITableView子类化?

pbx*_*pbx 5 cocoa-touch objective-c uitableview ios

我想继承一个UITableView,因为我想在我的应用程序中创建一个可重用的表视图组件.

这个想法是代替使用委托说cellForRowAtIndexPath我希望表视图本身能够获得该调用.

我不认为我想要一个UITableViewController,因为我想构建的UITableView必须存在于各种UIViewControllers中(并且这些UIViewController可能有自己的UITableViews).

我将我的UITableView子类化为:

@interface ShareUITableView : UITableView
Run Code Online (Sandbox Code Playgroud)

但它的方法都没有被调用.

我的ShareUITableView是通过将自定义类设置为ShareUITableView而通过NIB创建的.我已经在代码中验证了ShareUITableView的实例化.

我的UITableView没有委托给它的视图控制器,所以这不是问题.

有任何想法吗?

Ric*_*mpo 7

如果我理解你,你需要这个类声明:

@interface ShareUITableView : UITableView <UITableViewDataSource>
Run Code Online (Sandbox Code Playgroud)

然后,在类构造函数中,您应该将实例本身分配为自己的数据源:

- (id)init
{
    //...
    self.dataSource = self;
    //...
}
Run Code Online (Sandbox Code Playgroud)

当然,该课程必须采用该协议.

祝好运!


小智 5

MyTableView.h

// MyTableView.h

// This overrides the UITableViewDataSource with your own so you can add any methods   you would like.
@protocol MyTableViewDataSource <UITableViewDataSource>

@required
// This is where you put methods that are required for your custom table to work (optional)
- (int)myRequiredMethod;

@optional
// This is where you put methods that are optional, like settings (optional)

@end

// This overrides the UITableViewDelegate with your own so you can add any methods you would like.
@protocol MyTableViewDelegate <UITableViewDelegate>

@required
// This is where you put methods that are required for your custom table to work (optional)

@optional
// This is where you put methods that are optional, like settings (optional)

@end

// Make sure you add UITableViewDelegate and UITableViewDataSource implementations.
@interface MyTableView : UITableView <UITableViewDelegate, UITableViewDataSource> {

    // Your customer datasource and delegate.
    id <MyTableViewDataSource> myDataSource;
    id <MyTableViewDelegate> myDelegate;
}

@end
Run Code Online (Sandbox Code Playgroud)

MyTableView.m

// MyTableView.m

#import "MyTableView.h"

@implementation MyTableView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code

        // This is how you can use your custom method.
        int i = [myDataSource myRequiredMethod];
    }
    return self;
}

- (void)awakeFromNib {
    // This assigns the delegate and datasource you assigned to File's Owner in your xib to your custom methods
    myDataSource = (id<MyTableViewDataSource>)self.dataSource;
    myDelegate = (id<MyTableViewDelegate>)self.delegate;
    self.delegate = self;
    self.dataSource = self;
}

// This is an example of how to override an existing UITableView method.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    // This calls the method implemented in your ViewController. See Below.
    NSInteger rows = [myDataSource tableView:tableView numberOfRowsInSection:section];

    return rows;
}

@end
Run Code Online (Sandbox Code Playgroud)

MyViewController.h

// MyViewController.h

#import "MyTableView.h"

// Use MyTableViewDataSource and MyTableViewDelegate instead of UITableViewDataSource and UITableViewDelegate
@interface MyViewController : UIViewController <MyTableViewDataSource, MyTableViewDelegate> {

@end
Run Code Online (Sandbox Code Playgroud)

MyViewController.m

// MyViewController.m

#import "MyViewController.h"

@interface MyViewController ()

@end

@implementation MyViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
}

// This method will be overridden by myTableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (int)myRequiredMethod {
    return 2;
}
Run Code Online (Sandbox Code Playgroud)

子类化是制作可重用的自定义UI元素的好方法.


Vik*_*ica 2

我认为,你仍然应该使用 Controller 类。我希望子类化 UITableView 是一项繁琐的工作 \xe2\x80\x94 如果可能的话,并且数量合理。

\n\n

让 UIViewController/NoViewController 实现委托和数据源并将另一个控制器分配给特定的 tableView 是没有问题的。请注意,数据源和委托不需要是 UITableViewController 的子类。

\n\n

看看这个答案:在运行时实现委托?

\n\n
\n

我的 UITableView 没有委托给它的视图控制器,所以这不是问题。

\n
\n\n

您必须使用委托和数据源,这就是 TableView 的填充和配置方式。否则你将不得不覆盖 UITableView \xe2\x80\x94 的每个方法,包括私有方法,如果你想进入 AppStore,这是不行的。重新创建 UITableView 而不对其进行子类化会更加容易。

\n