【问题标题】:xcode storyboard nested table pass arrayxcode故事板嵌套表传递数组
【发布时间】:2013-05-10 18:05:58
【问题描述】:

我从互联网上获取了一个类似于 pulse 的应用程序示例脚本,该应用程序由 xib 文件组成,我将其构建为进入情节提要以利用水平表格滚动。

由于某种原因,tableView.m 中的“cell = tableViewCell”一直对我失败,断言失败 UITableView dataSource 必须从 tableView:cellForRowAtIndexPath 返回一个单元格

如果我注释掉“cell = tableViewCell”,程序运行不会失败,但我没有将信息传递给 tableViewCell。

有没有我看不到的简单解决方案?

tableView.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   static NSString *CellIdentifier = @"Cell";
   TableViewCell *cell = (TableViewCell*)[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];


       CGAffineTransform rotateTable = CGAffineTransformMakeRotation(-M_PI_2);
       tableViewCell.horizontalTableView.transform = rotateTable;
       tableViewCell.horizontalTableView.frame = CGRectMake(0, 0, tableViewCell.horizontalTableView.frame.size.width, tableViewCell.horizontalTableView.frame.size.height);

       tableViewCell.contentArray = [arrays objectAtIndex:indexPath.section];

       tableViewCell.horizontalTableView.allowsSelection = YES;
       cell = tableViewCell;

     return cell;
 } 

tableViewCell.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   static NSString *CellIdentifier = @"Cell";
   UITableViewCell *cell = [self.horizontalTableView dequeueReusableCellWithIdentifier:CellIdentifier];

   for (UIImageView *view in cell.subviews) {
       [view removeFromSuperview];
   }

   UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
   imageView.image = [contentArray objectAtIndex:indexPath.row]];
   imageView.contentMode = UIViewContentModeCenter;

   CGAffineTransform rotateImage = CGAffineTransformMakeRotation(M_PI_2);
   imageView.transform = rotateImage;

   [cell addSubview:imageView];

   return cell;
}

【问题讨论】:

  • 实际错误是“'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'

标签: xcode uitableview uistoryboard nested-table


【解决方案1】:

如果 dequeue 返回 nil,您应该只分配和初始化一个新的表格单元格。

if (!cell) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] ;
}

您使用的示例 http://iosstuff.wordpress.com/2011/06/29/creating-pulse-style-scrolling-horizontally-scrolling-uitableview-as-a-subview-of-uitableviewcell/ 确实很旧,适用于 iOS 4。去年在 iOS 6 中,Apple 添加了 Collection Views 来执行这些类型的事情。

你应该检查一下:

WWW2012 Session 205:介绍集合视图 https://developer.apple.com/videos/wwdc/2012/?id=205

第 7 讲集合视图 https://itunes.apple.com/us/course/coding-together-developing/id593208016

【讨论】:

  • 好的。但我怎么能将 contentArray 从 tableView 转移到 Horizo​​ntaltableView 单元格(嵌套表)
  • 我想我不知道你在做什么,你使用的例子的 url 是什么?
  • @gr 我更新了答案,示例真的很旧,有更好的方法可以做到这一点。
  • 使用集合视图会很好,但是关于让视图像“脉冲”应用程序或“应用商店特色项目”一样工作的信息有限。在 ios6 之前也可能无法正常工作
猜你喜欢
  • 1970-01-01
  • 2012-01-15
  • 1970-01-01
  • 1970-01-01
  • 2012-03-31
  • 1970-01-01
  • 2012-03-28
  • 2015-09-16
  • 2012-01-22
相关资源
最近更新 更多