iOS:reloadData不能正常工作

时间:2014-03-15 14:05:33

标签: ios uitableview reloaddata

我是整个iOS开发的新手,所以不太擅长。我目前有一个带有两个tableviews的viewController。第一个表始终可见并包含类别。只有在选择第一个表格中的单元格时,才能看到第二个表格。然后,第二个表包含在第一个tableview中选择的单元格的子项。两个tableviews都连接到.h文件中的属性。 Two tableviews inside the viewcontroller (storyboard)

viewcontroller本身连接到FoodAddViewController.h和FoodAddViewController.m文件。 在FoodAddViewController.h文件中,我有:

#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

对于FoodAddViewController.m,我有:

@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

现在,问题是reloadData方法无法正常工作。例如:如果我按下第一个tableview(Categorien)中的第一个单元格,则没有任何反应。但是当我单击另一个单元格时,第二个tableview(Invulling)将加载第一个单元格的结果。

2 个答案:

答案 0 :(得分:1)

您必须使用didSelectRowAtIndexPath而不是didDeselectRowAtIndexPath方法

另外查看一些关于调试的教程,你可能已经知道该方法不是 如果添加了断点则被调用

答案 1 :(得分:0)

您正在使用

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

相反,请使用

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:
相关问题