简体   繁体   English

tableview连接到数据源和委托-如何

[英]tableview is connected to datasource and delegate - how

i am new to iOS, this may seem like a basic question. 我是iOS的新手,这似乎是一个基本问题。 I have been working through this tutorial and I have no idea how the tableview is connected to the code. 我一直在研究本教程 ,但不知道tableview如何连接到代码。 The sample project can be downloaded here . 示例项目可以在此处下载

I was my understanding that you need to extend UITableViewDelegate, UITableViewDataSource in the code, then in the storyboard you can drag from the tableview to them. 我的理解是,您需要在代码中扩展UITableViewDelegate,UITableViewDataSource,然后在情节提要中将其从tableview拖到它们。

But what is perplexing is that the sample project does not extend UITableViewDelegate, UITableViewDataSource at all, therefore, how is the tableview in the story board connected to the code ? 但是令人困惑的是,示例项目根本没有扩展UITableViewDelegate,UITableViewDataSource,因此,情节提要中的tableview如何与代码连接?

Datasource is used to supply data and delegate is used to supply behaviour. 数据源用于提供数据,委托用于提供行为。 UITableView asks your datasource every time it needs data to display. UITableView每次需要显示数据时都会询问您的数据源。 It provides a lot of flexibility for how you choose to represent your underlying data model. 它为您选择表示基础数据模型提供了很大的灵活性。 You simply define specific methods to use in order to get table information, and iOS can call them when it needs to know something like the number of rows in a section, or the content of a particular row. 您只需定义用于获取表信息的特定方法,iOS就可以在需要了解某节中的行数或特定行的内容等信息时调用它们。

You will probably implement your own delegate mechanism in the future. 您将来可能会实现自己的委托机制。 It is a great design pattern which handles interaction/data transfer between objects. 这是一种出色的设计模式,可以处理对象之间的交互/数据传输。

Because, the basic class adopts from UITableViewController 因为,基本类从UITableViewController采用

@interface WTTableViewController : UITableViewController

In your storyboard you just ctrl-drag from tableview to viewController and choose delegate and dataSource. 在情节提要中,只需将ctrl从表视图拖动到viewController,然后选择委托和数据源。

If you working with TableViewController, UITableViewDelegate and UITableViewDataSource are connected to table automatically. 如果使用TableViewController,则UITableViewDelegateUITableViewDataSource将自动连接到表。 In case you are working with ViewControler which contains TableView, you have to add UITableViewDelegate and UITableViewDataSource like this: @interface myViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> . 如果使用包含TableView的ViewControler,则必须添加UITableViewDelegateUITableViewDataSource如下所示: @interface myViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> In new versions of Xcode you can drag delegate and data source to table, and if you want to set delegate programmatically, you can add this two lines of codes: 在新版本的Xcode中,您可以将委托和数据源拖到表中,如果要以编程方式设置委托,则可以添加以下两行代码:

[tableView setDelegate:self];
[tableView setDataSource:self];


or, equally: 或者,同样地:

tableview.delegate = self;
tableview.dataSource = self;

In viewcontroller.h file e declare the delegate and datasource method. 在viewcontroller.h文件中,声明委托和数据源方法。

UIViewController<UITableViewDataSource,UITableViewDelegate>

then connect the delegate and datasource method with viewController so in viewController.m file 然后将委托和数据源方法与viewController连接,以便在viewController.m文件中

- (void)viewDidLoad {
tableview.delegate=self;
tableview.dataSource=self;
}

In Objective-C/iOS you often implement things by having classes conform to protocols rather than subclassing classes. 在Objective-C / iOS中,您通常通过使类符合协议而不是对类进行子类化来实现事情。 WTTableViewController conforms to the protocols UITableViewDataSource: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewDataSource_Protocol/ and UITableViewDelegate: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewDelegate_Protocol/ WTTableViewController符合UITableViewDataSource协议: https : //developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewDataSource_Protocol/和UITableViewDelegate: https : //developer.apple.com/library/ios/documentation/UIKit/参考/ UITableViewDelegate_Protocol /

In MainStoryboard.storyboard you can see in the inspector to the right that the TableViewController has a custom class of WTTableViewController 在MainStoryboard.storyboard中,您可以在右侧的检查器中看到TableViewController具有WTTableViewController的自定义类

The relevant methods in WTTableViewController.m: WTTableViewController.m中的相关方法:

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"WeatherCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // Configure the cell...


    return cell;
}


#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
}

Here's some general info about protocols: https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/WorkingwithProtocols/WorkingwithProtocols.html 以下是有关协议的一些常规信息: https : //developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/WorkingwithProtocols/WorkingwithProtocols.html

I suggest reading a beginners book or tutorial on iOS to pick up the overall design principles otherwise many things can be quite confusing. 我建议阅读iOS上的初学者书籍或教程以了解总体设计原则,否则许多事情可能会令人困惑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM