xcode 如何在视图控制器中使用 TableView?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17534413/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use TableView inside viewcontroller?
提问by b3rge
In the storyboard I have added a table view to my view controller, I have ctrl dragged the TableView to to the Viewcontroller and connected "delegate" and "datasource". In the (.h) file I have added <UITableViewDataSource,UITableViewDelegate>but when I run the app I just get a SIGABRT error (?) and the app crashes. What should I do?
在情节提要中,我向视图控制器添加了一个表视图,我已按 ctrl 将 TableView 拖到 Viewcontroller 并连接“委托”和“数据源”。在我添加的 (.h) 文件中,<UITableViewDataSource,UITableViewDelegate>但是当我运行应用程序时,我只会收到一个 SIGABRT 错误 (?) 并且应用程序崩溃。我该怎么办?
回答by Levent Y?ld?z
So far so good, you just have to implement UITableViewDataSource and UITableViewDelegate in your implementation file.
到目前为止一切顺利,你只需要在你的实现文件中实现 UITableViewDataSource 和 UITableViewDelegate 。
Required functions are as follows;
所需功能如下;
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [regions count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Number of rows is the number of time zones in the region for the specified section.
Region *region = [regions objectAtIndex:section];
return [region.timeZoneWrappers count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
// The header for the section is the region name -- get this from the region at the section index.
Region *region = [regions objectAtIndex:section];
return [region name];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = @"MyReuseIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier]];
}
Region *region = [regions objectAtIndex:indexPath.section];
TimeZoneWrapper *timeZoneWrapper = [region.timeZoneWrappers objectAtIndex:indexPath.row];
cell.textLabel.text = timeZoneWrapper.localeName;
return cell;
}

