2

我正在处理 iPad 应用程序中的列表显示。

我有一个屏幕,屏幕上有一个UISegmentedController和一个UITableView

每个UITableViewControllers 都在一个类中定义,每个都有自己的 Delegate 和 DataSource 方法来访问和控制列表。有一个共享的超超类,以及定义的两个超类。

我以前在它们自己的屏幕中拥有列表,但希望将它们放在一个屏幕中并使用选择器在视图之间切换。

目前,我已将代码设置为在UITableViews 之间切换并尝试显示数据,并让代码以正确的方法调用正确的函数,但UITableView屏幕中没有出现数据。

我确信有一些“魔法”我没有应用到这个问题上,但是到目前为止我发现的任何例子中都没有“法术”。

如何在现有 TableView 中的 UITableViewController 和他们自己的 UITableViewCells 之间切换?

我正在尝试在 IOS 5.1 中执行此操作,并使用 Xcode 4.6.3 作为开发环境。

4

2 回答 2

0

这是我用来“交换” UITableViews 的代码:

-(void)selectView:(NSInteger)selected
{
SectionListViewController *listViewController;

switch (selected) {
    default:
    case 0:  // Animal by Name
        listViewController = [[AnimalNameListViewController alloc] init];
        break;

    case 1:  // Animal by Breeder
        listViewController = [[AnimalBreederListViewController alloc] init];
        break;

    case 2:  // Animal by Owner
        listViewController = [[AnimalOwnerListViewController alloc] init];
        break;

    case 3:  // Animal by Breed
        listViewController = [[AnimalBreedListViewController alloc] init];
        break;

    case 4:  // Animal by RegOrg
        listViewController = [[AnimalRegOrgListViewController alloc] init];
        break;

    case 5:  // Breeder by Name
        listViewController = [[BreederNameListViewController alloc] init];
        break;
}

[listViewController initalizeTableListData];

[self.listView setDataSource:listViewController];
[self.listView setDelegate:listViewController];
self.listView = listViewController.tableView;

[listViewController.tableView reloadData];
}

它“几乎”有效。调用了 Class 中的 Delegate 和 DataSource 例程,但 View 中没有数据。

于 2014-06-03T05:40:03.980 回答
0

您可以使用一个 UITableView 来显示所有数据。您可以使用 UIButtons 而不是使用 UISegmentedControl。在 UIButton 的 IBAction 中,您可以更改要显示的特定数据的 dataArray。喜欢

self.dataArray = array1;

[self.tableview reloadData];

并使用此 dataArray 在您的 UITableView 方法中显示数据。

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    .....
}

或者

您可以使用我在我的一个应用程序中使用的不同方法。

#pragma mark - UITableView Delegate and DataSource

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

        if (btnFavourites.selected) {
            return arrFavourites.count;
        }else if (btnHistory.selected){
            return arrHistories.count;
        }else if (btnHome.selected){
            return arrAddresses.count;
        }

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        static NSString *cellIdentifier = @"MyCell";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        }

        // Configure the cell...
        if (btnHome.selected) {
            Home *home = [arrAddresses objectAtIndex:indexPath.row];
            cell.textLabel.text = home.address;//[address valueForKey:@"address"];

        }else if (btnFavourites.selected){
            Favourites *favourite = [arrFavourites objectAtIndex:indexPath.row];
            cell.textLabel.text = favourite.location;//[favourite valueForKey:@"address"];

        }else if (btnHistory.selected){
            History *history = [arrHistories objectAtIndex:indexPath.row];
            cell.textLabel.text = history.location;// [history valueForKey:@"address"];

        }


        return cell;

}

您可以根据您的要求进行定制。希望这可以帮助。谢谢。:)

于 2014-06-02T07:31:33.537 回答