【问题标题】:UITableView does not load dataUITableView 不加载数据
【发布时间】:2016-01-27 20:03:36
【问题描述】:

我对 iOS 开发比较陌生。现在,我使用情节提要创建了 2 个ViewController。一个包含一个按钮,该按钮使用 segue(显示)通向另一个控制器。

这个控制器是嵌入在导航控制器中的TableViewController,并且已经连接到它的负责类,该类继承自UITableViewController

我的问题是这个页面没有加载已经在 viewDidLoad 中初始化的数据 (NSMutableArray) 使用

_dataClient = [NSMutableArray arrayWithObjects:@"First City",@"Second City",@"Third City",@"Forth City",@"Fift City", nil];

这是我的表委托和数据源:

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 0;
}

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

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

    // Configure the cell...
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }

    cell.textLabel.text = [_dataClient objectAtIndex:indexPath.row];

    return cell;
}

有没有我忘记做的步骤?

【问题讨论】:

  • don'take -numberOfSectionsInTableView 方法默认为 1,如果您想要超过 1 个部分,那么您可以采用该方法并根据需要返回部分。
  • 是的,我想我很少使用节超过一个的表,除了静态表。但是谢谢@bhavin ramani。

标签: ios objective-c iphone uitableview storyboard


【解决方案1】:

必须至少返回 1 个部分。更改此行:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1; /// here
}

【讨论】:

  • 哇哦,你救了我的命 anhtu!非常感谢!
  • 剩余 7 分钟 :D
  • 你们太棒了!谢谢 Kumar KL 和@anthu
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-05
  • 1970-01-01
  • 2023-03-31
  • 1970-01-01
  • 2013-11-13
  • 1970-01-01
相关资源
最近更新 更多