문제

I'm new to the whole iOS developement, so not really good at it. I currently have a viewController with two tableviews. The first table is always visible and contains categories. The second table should only be visible when a cell from the first table is selected. The second table then contains subitems from the cell that was selected in the first tableview. Both tableviews are connected to their property in the .h file. Two tableviews inside the viewcontroller (storyboard)

The viewcontroller itself is connected to a FoodAddViewController.h and FoodAddViewController.m file. In the FoodAddViewController.h file, I have:

#import <UIKit/UIKit.h>

@class FoodAddViewController;


@interface FoodAddViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>
@property (strong, nonatomic) IBOutlet UITableView *Categorien;
@property (strong, nonatomic) IBOutlet UITableView *Invulling;

- (IBAction)cancel:(id)sender;

@end

For the FoodAddViewController.m, I have:

@interface FoodAddViewController ()
{
    NSArray *CategoryLabel;
    NSMutableArray *elementen;
}

@end

@implementation FoodAddViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.Categorien.layer.transform = CATransform3DRotate(CATransform3DIdentity,-1.57079633,0,0,1);
    self.Categorien.frame=CGRectMake(0,200, 700,234);
    [self.Invulling setHidden:TRUE];
    self.Invulling.layer.transform = CATransform3DRotate(CATransform3DIdentity,-1.57079633,0,0,1);
    self.Invulling.frame=CGRectMake(0,200, 700,234);
    self.Categorien.dataSource = self;
    self.Categorien.delegate = self;
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"categorie" ofType:@"txt"];
    NSString *content = [NSString stringWithContentsOfFile:filePath
                                                  encoding:NSMacOSRomanStringEncoding
                                                     error:NULL];
    NSArray * categories = [content componentsSeparatedByString:@"\n"];
    CategoryLabel = categories;
}
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(self.Categorien==tableView) {
        return CategoryLabel.count;
    }
    if(self.Invulling==tableView) {
        return elementen.count;
    }
    return 0;
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(self.Categorien==tableView) {

        static NSString *CellIdentifier = @"Cell";
        CustomCell *Cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (!Cell) {
        Cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
        Cell.layer.transform = CATransform3DRotate(CATransform3DIdentity,1.57079633,0,0,1);
        Cell.frame=CGRectMake(0,0,234,150);

        Cell.textLabel.text = [CategoryLabel objectAtIndex:indexPath.row];

        return Cell;
    }
    if(self.Invulling==tableView) {
        static NSString *CellIdentifier = @"DetailCell";
        CustomCell *Cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (!Cell) {
            Cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
        Cell.layer.transform = CATransform3DRotate(CATransform3DIdentity,1.57079633,0,0,1);
        Cell.frame=CGRectMake(0,0,234,150);

        Cell.textLabel.text = [elementen objectAtIndex:indexPath.row];

        return Cell;
    }
    return NULL;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 200;
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.Invulling.dataSource = self;
    self.Invulling.delegate = self;

    elementen = [[NSMutableArray alloc]init];
    if(self.Categorien==tableView) {
        UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
        NSString *cellText = selectedCell.textLabel.text;
        PFQuery *query = [PFQuery queryWithClassName:cellText];
        [query selectKeys:@[@"Naam"]];
        NSArray *result = [query findObjects];
        for (PFObject *object in result) {
            NSString *naam = object[@"Naam"];
            if(![elementen containsObject:naam]) {
                [elementen addObject:naam];
            }
        }
    }
    [self.Invulling setHidden:FALSE];
    [self.Invulling reloadData];
}
end

Now, the problem is that the reloadData method does not work correctly. For example: if I press the first cell from the first tableview (Categorien), then nothing happens. But when I click another cell, the second tableview (Invulling) gets loaded with the results from the first cell.

도움이 되었습니까?

해결책

you have to use didSelectRowAtIndexPath not didDeselectRowAtIndexPath method

Also look up some tutorial on debugging, you could have known that the method is not being called if you have added breakpoints

다른 팁

You are using

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:

Instead, use

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top