我正在开发一个具有UITableView,两个数组和一个UISegmentedControl的应用程序。现在,我需要将UISegmentedControl的值为0时从数组1加载到UITableView数据中,以及当UISegmentedControl的值为1时从数组2加载数据。简单来说,我需要切换要从数组加载到UITableView中的数据。我尝试使用bool,但是它不起作用,我也认为它不理想。
这是我的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
allTableData = [[NSMutableArray alloc] initWithObjects:
[[Food alloc] initWithName:@"BWM" andDescription:@"Auto" andDefinice:@"Osobni"],nil ];
allTableData2 = [[NSMutableArray alloc] initWithObjects:
[[Food alloc] initWithName:@"Renault" andDescription:@"Dodavka" andDefinice:@"Velka"],nil ];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
long rowCount;
if(self.isSwich == false)
rowCount = allTableData.count;
else
rowCount = allTableData2.count;
return rowCount;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
Food* food;
if(self.isSwitch == false)
{
food = [allTableData objectAtIndex:indexPath.row];
}
else
{
food = [allTableData2 objectAtIndex:indexPath.row];
}
cell.textLabel.text = food.name;
cell.detailTextLabel.text = food.description;
return cell;
}
-(IBAction)switchdata:(id)sender
{
if(self.myswitcher.selectedSegmentIndex == 0)
{
isSwitch = FALSE;
}
else
{
isSwitch = TRUE;
}
}
最佳答案
您缺少reloadData
实现中对witchdata:
的调用。添加此 call 将解决此问题。
我也认为这并不理想。
那就对了。与其存储一个说要用于数据源的数组的标志,不如存储该数组本身。这将消除if
属性上的所有isSwitch
:
// Declare this as an instance variable, and use it
// in your data source methods.
NSArray *theSource;
- (void)viewDidLoad {
[super viewDidLoad];
allTableData = [[NSMutableArray alloc] initWithObjects:
[[Food alloc] initWithName:@"BWM" andDescription:@"Auto" andDefinice:@"Osobni"],nil ];
allTableData2 = [[NSMutableArray alloc] initWithObjects:
[[Food alloc] initWithName:@"Renault" andDescription:@"Dodavka" andDefinice:@"Velka"], nil];
theSource = allTableData;
}
-(IBAction)switchdata:(id)sender {
if(self.myswitcher.selectedSegmentIndex == 0) {
theSource = allTableData;
} else {
theSource = allTableData2;
}
[myTable reloadData];
}
或者,您可以将
isSwitch
设为整数,然后将其用作数组的索引,该数组的allTableData
的索引为零,而allTableData2
的索引为1:NSArray *sources;
int sourceIndex;
// Use sources[sourceIndex] as the current source
- (void)viewDidLoad {
[super viewDidLoad];
allTableData = [[NSMutableArray alloc] initWithObjects:
[[Food alloc] initWithName:@"BWM" andDescription:@"Auto" andDefinice:@"Osobni"], nil];
allTableData2 = [[NSMutableArray alloc] initWithObjects:
[[Food alloc] initWithName:@"Renault" andDescription:@"Dodavka" andDefinice:@"Velka"],nil ];
sources = @[allTableData, allTableData2];
sourceIndex = 0;
}
-(IBAction)switchdata:(id)sender {
sourceIndex = self.myswitcher.selectedSegmentIndex;
[myTable reloadData];
}
关于ios - UITableView-从两个数组切换数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29634458/