【发布时间】:2015-04-14 18:08:20
【问题描述】:
我正在开发一个应用程序,其中有一个 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;
}
}
【问题讨论】:
-
您能否更详细地解释一下“不起作用”部分?
-
数据不切换。 UITableView 显示来自一个数组的数据,但是当我单击 UISegmentedControl 时没有任何反应。
标签: ios objective-c arrays uitableview