【发布时间】:2015-03-26 05:31:39
【问题描述】:
我在 Storyboard 中创建了一个 UITableView,它是动态单元格。问题是当没有足够的单元格可以重复使用时,它会随机清空我的一些单元格。我认为这是合乎逻辑的,但我想解决这个问题。
我举个例子: 我有一个 UITableView 能够在一个视图中生成 10 个单元格。但是现在,我只想显示 10 个单元格中的 8 个单元格。当我只有一个部分时,它没有问题。超过 1 个部分,它总是会清空 2 个单元格并显示 8 个单元格,但它应该显示 10 个单元格。
谁能帮我解决这个问题?谢谢。
已更新源代码
#pragma mark - List View
#pragma mark - Table View Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
- (NSInteger)tableView:(UITableView *)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 *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)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 对象数组。
已更新图片:
【问题讨论】:
-
您“想在不滚动视图的情况下显示 10 个单元格中的 8 个”?
-
实际上没有。我的表格视图可以显示 8 个单元格而无需滚动。我的意思是当有多个部分时,有 2 个空单元格,但它假设显示 10 个单元格,这会导致错误。
-
发布您如何填充单元格的代码。
-
当您的数据源委托返回 1 时,您为什么会得到 2 个部分?
-
不知道是否与问题有关。在这些行中 for (id object in cell.contentView.subviews) { [object removeFromSuperview]; } [cell.contentView addSubview:[self.listCollection objectAtIndex:indexPath.row]];为什么不创建一个包含这些子视图的自定义单元格?
标签: ios objective-c iphone uitableview