简体   繁体   中英

UITableViews and UITableViewCells

I am not sure what I am doing wrong but if anyone can point me in the right direction I would appreciate it. I am creating a app in xcode. I currently have two buttons that segue to different view controllers both view are controlled by the same class. There are separate views defined in the class. I also have two arrays one for each view which is populated with the information I want to show in the view. here is the code from the viewcontrollers .m file. If i remove the breakpoint that appears both buttons function however the second button shows the information from the wrong array but goes to the correct view controller. I am completely stumped.

#import "techTwoViewController.h"
#import "maintInstrctViewController.h"
#import "showTechTipsViewController.h"
@interface techTwoViewController ()

@end

@implementation techTwoViewController
{
    NSArray *maintInstrct;
    NSArray *techTips;
}
@synthesize tableView;
@synthesize techTableView;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Initialize table data
    maintInstrct = [NSArray arrayWithObjects:@"One", @"Two", nil];

    techTips = [NSArray arrayWithObjects:@"Three", @"Four", nil];    
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (NSInteger)techTableView:(UITableView *)techTableView numberOfRowsInSection:(NSInteger)section
{
    return [techTips count];
}

- (UITableViewCell *)techTableView:(UITableView *)techTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTechTableIdentifier = @"TechTipsCell";

    UITableViewCell *cell = [techTableView dequeueReusableCellWithIdentifier:simpleTechTableIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTechTableIdentifier];
    }

    cell.textLabel.text = [techTips objectAtIndex:indexPath.row];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [maintInstrct count];
}

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }

    cell.textLabel.text = [maintInstrct objectAtIndex:indexPath.row];
    return cell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"showMaintKitInstrct"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        maintInstrctViewController *destViewController = segue.destinationViewController;
        destViewController.maintKitName = [maintInstrct objectAtIndex:indexPath.row];
    }

    else if ([segue.identifier isEqualToString:@"showTechTips"]) {
        NSIndexPath *indexPath = [self.techTableView indexPathForSelectedRow];
        showTechTipsViewController *destViewController = segue.destinationViewController;
        destViewController.techTipName = [techTips objectAtIndex:indexPath.row];
    }
}
@end

I see whats going on here. You are assigning this view controller as the data source for both of the UITableViews it owns. But when your techTableView calls its dataSource , it has no way of knowing that you want it to call your custom dataSource methods. You need to give your techTableView its own dataSource . A separate class that conforms to UITableViewDataSource protocol and implements the necessary methods.

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