【问题标题】:Creating a UITableView class and assign to storyboard创建一个 UITableView 类并分配给故事板
【发布时间】:2018-01-29 20:22:11
【问题描述】:

是否可以在情节提要中创建一个 UITableview 类并分配给 UITableview?我有这个例子,但委托方法不调用。并且表格显示为空。

在 Table.h 类中:

#import <UIKit/UIKit.h>

@interface Table : UITableView <UITableViewDataSource,UITableViewDelegate>

@end

在 Table.m 类中:

@implementation Table{

    NSArray *data;
}

-(instancetype)init{

    self = [super init];
    if (self) {
        self.delegate= self;
        self.dataSource = self;
        data = @[@"bmw",@"mercedes",@"audi"];
    }
    return self;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [data count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    cell.textLabel.text = [data objectAtIndex:indexPath.row];
    return cell;
}

最后,在我的 ViewController.m 中,我需要实现它。 我有这个代码:

    #import "ViewController.h"
    #import "Table.h"

    @interface ViewController ()
    @end

    @implementation ViewController 

    - (void)viewDidLoad {
        [super viewDidLoad];
        Table *mainTable = [Table new];
        self.table = mainTable;
    }
    @end

【问题讨论】:

  • 我必须假设ViewControllerUITableViewController 的子类。您的 Storyboard 设置如何?您是否覆盖了“插入式”控制器的类(您在那里使用了哪个类/组件)?
  • 是的,ViewController 是 UITableViewController 的子类,在我的故事板中,我有一个 UITableview 分配给类表。我尝试自定义一个 TableView,以便它可以在任何地方重复使用。
  • 正如@GeneCode 提到的 - 通常你会有一个“控制器”来实现表源/委托,因为控制器将数据提供给它的视图 - 表。在 Storyboard 中,您需要有一个 UIViewController 组件,您可以在其中将类覆盖到您的 ViewController

标签: ios objective-c uitableview delegates datasource


【解决方案1】:

我认为您误解了这里的概念。我不确定您要完成什么 - 将 TableView 子类化(即自定义一个 TableView 以便它可以在任何地方重用),或者创建您自己的 TableViewController。委托将在控制器上实现,而不是在对象本身上。所以你的 self.delegate=self 是错误的,没有意义。

【讨论】:

  • 正如你所说,我尝试自定义一个 TableView,以便它可以在任何地方重复使用。如果您有小费,我将不胜感激。
【解决方案2】:

如何为tableView创建Sublass

   // 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


        //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

        //MyViewController.h







    #import "MyTableView.h"

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

        @end


       // 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;
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-16
    • 1970-01-01
    • 1970-01-01
    • 2017-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-17
    相关资源
    最近更新 更多