• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 没有足够的单元格在 UITableView 中使用

[复制链接]
菜鸟教程小白 发表于 2022-12-13 06:05:33 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我在 Storyboard 中创建了一个 UITableView,它是动态单元格。问题是当没有足够的单元格可以重复使用时,它会随机清空我的一些单元格。我认为这是合乎逻辑的,但我想解决这个问题。

我举个例子: 我有一个 UITableView 能够在一个 View 中生成 10 个单元格。但是现在,我只想显示 10 个单元格中的 8 个单元格。当我只有一个部分时,它没有问题。超过 1 个部分,它总是会清空 2 个单元格并显示 8 个单元格,但它应该显示 10 个单元格。

谁能帮我解决这个问题?谢谢。

已更新源代码

#pragma mark - List View
#pragma mark - Table View Delegate
- (NSInteger)numberOfSectionsInTableViewUITableView *)tableView {
   return 2;
}

- (NSInteger)tableViewUITableView *)tableView numberOfRowsInSection:   (NSInteger)section {
   // Number of rows is the number of time zones in the region for the          specified section.
  return self.listCollection.count;
}

- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath {

   UITableViewCell *cell = [tableView  dequeueReusableCellWithIdentifier"ListCell" forIndexPath:indexPath];

   for (id object in cell.contentView.subviews) {
       [object removeFromSuperview];
   }

   [cell.contentView addSubview:[self.listCollection   objectAtIndex:indexPath.row]];

   return cell;

}

self.listCollection 有一个 UIView 对象数组。

已更新图片:

  1. Image 1
  2. Image 2



Best Answer-推荐答案


发生这种情况是因为您使用了 2 个部分,但没有分别指定每个部分的内容。要理解这一点,我们需要查看 Apple DocumentationaddSubview: 方法的描述。

This method establishes a strong reference to view and sets its next responder to the receiver, which is its new superview.

Views can have only one superview. If view already has a superview and that view is not the receiver, this method removes the previous superview before making the receiver its new superview.

仔细查看第二段中的粗体部分。由于您使用 listCollection 中的相同 View 对象来填充这两个部分,因此最新创建的单元格将成为该 View 对象的 superview 并且前一个单元格将被省略只不过是飞机contentView。您可以通过为单元格 contentView 指定一些默认颜色来获得真实的感觉。您的空白单元格将显示完整内容的颜色,而其他单元格将显示 view

cell.contentView.backgroundColor = [UIColor greenColor];

解决方案
我将建议对这两个部分使用 2 个不同的数据源,如下所述。

-(NSInteger)tableViewUITableView *)tableView numberOfRowsInSection:   (NSInteger)section {
   // Number of rows is the number of time zones in the region for the          specified section.  
   if(section==0)
      return self.listCollection.count; 
   else   
      return <row count for section 1>  
 }

您需要为每个部分修改 cellForRowAtIndexPath: 代码。

-(UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath {

   UITableViewCell *cell = [tableView  dequeueReusableCellWithIdentifier"ListCell" forIndexPath:indexPath];

   for (id object in cell.contentView.subviews) {
       [object removeFromSuperview];
   }
   if(section==0)
      [cell.contentView addSubview:[self.listCollection   objectAtIndex:indexPath.row]];
   else
      [cell.contentView addSubview:[<second array of views>   objectAtIndex:indexPath.row]];

   return cell;
}

但是,如果您必须使用相同的数组,那么您可以使用这种技术。感谢 Rishi为此。
最后但同样重要的是,您应该使用 Michal Zygar 在他的帖子中建议的自定义 UITableViewCell 类。

UIView *tempView = [self.listCollection objectAtIndex:indexPath.row];
NSData *tempArchiveView = [NSKeyedArchiver archivedDataWithRootObject:tempView];
UIView *viewOfSelf = [NSKeyedUnarchiver unarchiveObjectWithData:tempArchiveView];
[cell.contentView addSubview:viewOfSelf];

关于ios - 没有足够的单元格在 UITableView 中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29271680/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap