【问题标题】:How to use TableView inside viewcontroller?如何在 viewcontroller 中使用 TableView?
【发布时间】:2013-07-06 05:51:54
【问题描述】:

在情节提要中,我向视图控制器添加了一个表格视图,我已经 ctrl 将 TableView 拖到 Viewcontroller 并连接了“委托”和“数据源”。在 (.h) 文件中,我添加了 <UITableViewDataSource,UITableViewDelegate>,但是当我运行应用程序时,我只收到一个 SIGABRT 错误 (?) 并且应用程序崩溃。我该怎么办?

【问题讨论】:

  • th 错误:#import #import "AppDelegate.h" int main(int argc, char *argv[]) { @autoreleasepool { return UIApplicationMain(argc, argv,无,NSStringFromClass([AppDelegate 类])); } }
  • 你实现了UITableViewDataSource所需的方法了吗?
  • 不,我没有@ColdLogic

标签: objective-c xcode uiviewcontroller storyboard tableview


【解决方案1】:

到目前为止一切顺利,您只需在实现文件中实现 UITableViewDataSource 和 UITableViewDelegate。

所需功能如下;

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

Here's the link for the Apple documentation

【讨论】:

  • 谢谢,这对我帮助很大!
猜你喜欢
  • 1970-01-01
  • 2012-09-14
  • 1970-01-01
  • 2012-08-22
  • 1970-01-01
  • 2020-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多