简体   繁体   中英

TableView ---> DetailView

I would like the organization names on TableView , and find more information on DetailView . But I do not know where is error, DetailView is not find my information. Here is the didSelectRow code:

TableView :

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    switch (section) {
        case 0:
            return 2;
            break;
        case 1:
            return 2;
            break;
        case 2:
            return 2;
            break;
    }
    return 0;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    NSString *sectionHeader = nil;    if(section == 0) {
        sectionHeader = @"Red Wine";
    }
    if(section == 1) {
        sectionHeader = @"White Wine";
    }
    if(section == 2) {
        sectionHeader = @"Sparkling Wine";
    }
    return sectionHeader;
}

- (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];
        [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
        [cell.textLabel setNumberOfLines:3];
    }
    switch (indexPath.section) {
        case 0: 
            switch (indexPath.row) {
                case 0:
                    cell.textLabel.text = @"Black Wattle Mt Benson Merlot 2008";
                    cell.detailTextLabel.text = @"Mt Benson, South Australia.";
                    break;
                case 1:
                    cell.textLabel.text = @"Two Hands Canny Butcher Barossa Valley Shiraz Grenache Mataro 2009";
                    cell.detailTextLabel.text = @"Barossa Valley, South Australia.";
                    break;
            }
            break;
        case 1: 
            switch (indexPath.row) {
                case 0:
                    cell.textLabel.text = @"Amberton Lizard Sauvignon Blanc Semillon 2011";
                    cell.detailTextLabel.text = @"South Eastern Australia.";
                    break;
                case 1:
                    cell.textLabel.text = @"Vasse Felix Margaret River Chardonnay Margaret River";
                    cell.detailTextLabel.text = @"Western Australia.";
                break;          }
            break;
        case 2: 
            switch (indexPath.row) {
                case 0:
                    cell.textLabel.text = @"Janisson Fils Brut Non Vintage Champagne";
                    cell.detailTextLabel.text = @"Champagne, France.";
                    break;
                case 3:
                    cell.textLabel.text = @"Francois Montand Brut Blanc De Blancs NV";
                    cell.detailTextLabel.text = @"Premium French sparkling vineyard areas.";
                    break;
            }
            break;
    }
    return cell;
}

-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    DetailViewController *ViewController = [[DetailViewController alloc] init];

    ViewController.Wine = [indexPath section];

    [self.navigationController pushViewController:ViewController animated:YES];
}

DetailView :

- (void)viewDidLoad{    
[super viewDidLoad];
switch (Wine) {
    case 0: 
        switch (Wine) {
            case 0:
                self.navigationItem.title = @"Black Wattle Mt Benson Merlot 2008";
                WineTextView.text = @"Alcohol: 14.5%\n"
                "\n"
                "Foods: Pan grilled fillet mignon with buttered mushrooms and silky mashed potatoes.\n";
                break;
            case 1:
                self.navigationItem.title = @"Two Hands Canny Butcher Barossa Valley Shiraz Grenache Mataro 2009";
                WineTextView.text =  @"Alcohol: 14.2%\n"
                "\n"
                "Foods: Enjoy with beef stew and winter vegetables.\n";
                break;

            default:
                break;
        }
        break;

    case 1: 
        switch (Wine) {
            case 0:
                self.navigationItem.title = @"Amberton Lizard Sauvignon Blanc Semillon 2011";
                WineTextView.text = @"Alcohol: 14.5%\n"
                "\n"
                "Foods: Pan grilled fillet mignon with buttered mushrooms and silky mashed potatoes.\n";
                break;
            case 1:
                self.navigationItem.title = @"Vasse Felix Margaret River Chardonnay Margaret River";
                WineTextView.text = @"Alcohol: 12%\n"
                "\n"
                "Foods: Enjoy with Chinese roast duck salad.\n";
                break;
            default:
                break;
        }
        break;

    case 2: 
        switch (Wine) {
            case 0:
                self.navigationItem.title = @"Janisson Fils Brut Non Vintage Champagne";
                WineTextView.text = @"Alcohol: 12%\n"
                "\n"
                "Foods: Ideal aperitif style, to accompany canapés and hors doeuvres.\n";
                break;
            case 1:
                self.navigationItem.title = @"Francois Montand Brut Blanc De Blancs NV";
                WineTextView.text = @"Alcohol: 12%\n"
                "\n"
                "Foods: Apéritif, fish, and creamy dishes..\n";
                break;
            default:
                break;
        }
    default:
        break;
}

At least your code in DetailView has two nested switch statements that switch on the same variable Wine . This for example leads to the "Two Hands Canny Butcher Barossa Valley" to be unreachable as Wine cannot be zero and one at the same time. Perhaps you meant to use two variables and store both section and row for use in DetailView .

In any case I would recommend to remove data duplication and store everything in one place. If you, for example, stored all your wine information in an NSArray you could use this array to retrieve the data without using a large case statement. This also would allow to exchange the fixed list for something retrieved from a local database or even the web later.

If you, for example, created your own class WineInformation which contains properties for every aspect of the wine (name, alcohol content, recommended foods) you could simply pass the WineInformation object for the selected row to your DetailView which would then display the detailed information. This way you don't have to handle index paths or any of that stuff in DetailView and just concentrate of displaying the handed wine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM