ios - UITableView-从两个数组切换数据

标签 ios objective-c arrays uitableview

我正在开发一个具有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/

相关文章:

ios - UITextField 在开始编辑时向左移动文本

objective-c - UIBezierPath 绘制直线

ios - 在应用程序运行时在 AppDelegate 中运行 didFinshLaunchingWithOptions?

c++ - 指向数组的指针,然后获取完整的数组 C++

javascript - 在 mongo shell 中使用 printjson 进行 mongoDB 调试

java - 如何将二维数组中的行元素相加Java

ios - iOS 上的应用内购买可以在后端服务器上完成吗?

ios - 使用来自另一个操作的现有展开转场

objective-c - iOS imageWithData 调整大小为 640 x 480

objective-c - NSView 上以椭圆为内容的鼠标事件