我会亲自创建一个从中加载数据的数组。把它放在你的实现中:
NSArray * _tableData
然后在您的 viewDidLoad 中将其分配给我们希望它开始的数组。
_tableData = [[NSArray alloc] initWithArray:allItems];
这最初会加载我们将始终看到的数据,因为段控件从索引 0 开始。我们必须在某处设置初始数据,以便 tableView 加载其中的一些数据。
然后设置行数和cellForRowAtIndex从_tableData数组中提取
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _tableData.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView_ cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell * cell = [tableView_ dequeueReusableCellWithIdentifier:bCell];
// Here we use the specific array as we would normally
return cell;
}
这一步意味着 tableView 将与数组一起加载。即使数组为空,视图仍会加载,因为单元格的数量为零。
现在在我们的值更改函数中,我们可以根据需要重置数组:
- (IBAction)segmentControlChanged:(UISegmentedControl *)sender {
if (sender.selectedSegmentIndex == 1) {
_tableData = allItems;
}
else {
_tableData = specialItems;
}
[self.tableView reloadData];
}
您只需要确保更改的段控件已链接到 XIB 文件中(或以编程方式),并在选择数组后重新加载表。
这种事情其实很容易做到。如果您遇到问题,我绝对会建议您逐步完成。在应用下一个步骤之前确保每个步骤都有效:
- 分别使用两组数据获取 tableView 加载
- 确认点击时段控件正在调用更改函数
那么就应该这样做