0

这是我第一次使用 Storyboard 构建应用程序。我正在尝试使用自定义单元格创建 UITableView。我在 IB 中创建了单元格并为其创建了自定义 tableViewCell 类(添加了一些标签,创建了适当的插座,在 IB 中连接它们并将自定义类分配给 IB 中的自定义单元格)。

在负责 TableView 的 ViewController 中,我创建了数据源(带有一些数组的 dict)并填写了所有必需的方法(我已经尝试使用 UITableViewControler 和设置为 tableview 的委托和数据源的 UIViewController )

当我运行应用程序时 - 表格视图是空的。在做了一些 NSLogging 之后,我注意到数据源方法永远不会执行。我不知道为什么。

我现在为此疯狂了几个小时。请帮帮我:)

如果您需要查看代码或情节提要或其他任何内容,请告诉我。

更新:好的,经过一番挖掘,我决定测试相同的代码,但使用 NSMutableArray 作为数据源而不是 NSMutableDictionary。它有效!现在,有人可以向我解释为什么它不适用于 dict 吗?

这是做了什么。我有一个包含 5 个数组的字典,每个数组有 5 个字符串。

在 numberOfRowsForSection 方法中,我返回了 [dict count]

在 cellForRowAtIndexPath 方法中,我使用了这段代码

NSArray * routeArray = [dealsDict objectForKey:@"route"];
cell.routeName.text = [routeArray objectAtIndex:indexPath.row];

NSArray * companyArray = [dealsDict objectForKey:@"company"];
cell.companyName.text = [companyArray objectAtIndex:indexPath.row];

NSArray * priceArray = [dealsDict objectForKey:@"price"];
cell.priceLabel.text = [priceArray objectAtIndex:indexPath.row];

NSArray * dateArray = [dealsDict objectForKey:@"date"];
cell.dateLabel.text = [dateArray objectAtIndex:indexPath.row];

NSArray * monthArray = [dealsDict objectForKey:@"month"];
cell.monthLabel.text = [monthArray objectAtIndex:indexPath.row];
NSLog(@"I'm in here");

return cell;

为什么它不想显示任何东西?

4

2 回答 2

0

在 IB 中,选择您的表格视图,并确保为您的控制器设置了delegate和出口dataSource

于 2013-02-05T18:23:09.493 回答
0

UITableView 需要一些集合数据集作为数据源。它的委托方法“cellForRowAtIndexPath”被调用等于集合元素的数量(ARRAY)。像 numberOfRowsInSection 委托告诉表视图它需要为集合中的每个元素调用 cellForRowAtIndexPath。并且它还需要一些迭代器来每次读取下一个元素,如果是字典,它总是在每次调用 cellForRowAtIndexPath 时坚持相同的数据元素。我希望它能给你它工作的想法,如果你还需要知道什么,请告诉我。

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

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath  *)indexPath
{
  MessagesCell *cell = [tableView dequeueReusableCellWithIdentifier:@"messageCellIdentifier"];

  Message *msg = [self.data objectAtIndex:indexPath.row];
  cell.Name.text = msg.Name;
  cell.Message.text = msg.MessageText;
  cell.Time.text = msg.Time;

  return cell;
}
于 2013-02-05T19:22:46.017 回答