Tableview不会触发cellForRowtIndexPath方法

时间:2013-05-21 11:12:38

标签: iphone ios objective-c uitableview

我的情况:

  • 我在故事板中连接了datasourcedelegate
  • 我已经获得了我的UITableViewCell和标识符以及代码定义等等。
  • 我已array获取了UITableView的数据,在调试中我的数据array已填入viewDidload方法

所以在我到达cellForRowIndexPathnumberOfRowsInSection之前,一切似乎都是正确的。它没有进入这些方法。当我  进一步运行,调试完成,没有任何故障,只有黑色  UITableView我什么也没有。我的阵列真的很奇怪  充满了数据,一切都按照应有的方式连接起来。

需要一些帮助,我现在正在寻找几天!

如果需要帮助代码,请问我。

我的代码:

m.file:

#import "DagprogrammaDetailViewController.h"

@interface DagprogrammaDetailViewController ()
@end

@implementation DagprogrammaDetailViewController
{
    NSMutableArray *DagprogrammaDetail;
}

@synthesize menuNaam;
@synthesize tableDagprogrammaDetail;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.title = menuNaam;

    NSArray *dagarray = [menuNaam componentsSeparatedByString:@" "];
    NSString *plaatsString = [dagarray objectAtIndex:1];
    plaatsString = [plaatsString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

    //object ophalen
    NSMutableString *urlString = [NSMutableString stringWithFormat:@"http://zesdaagse.mobi-app.be/WCFUrl/MobileService.svc/GetDagprogramma"];
    NSURL *url = [NSURL URLWithString:urlString];
    NSData *data = [NSData dataWithContentsOfURL:url];
    NSError *error;

    NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

    NSMutableArray *id = [NSMutableArray array];

    for(NSDictionary *item in json)
    {
        [id addObject:[item objectForKey:@"id"]];
    }

    int plaats = [plaatsString intValue];
    plaats -= 1;
    //id van de dag ophalen
    NSString *idDagprogramma = [id objectAtIndex:plaats];

    urlString = [NSMutableString stringWithFormat:@"http://zesdaagse.mobi-app.be/WCFUrl/MobileService.svc/GetDagprogrammaWedstrijden?id="];
    [urlString appendString:[NSString stringWithFormat:@"%@",idDagprogramma]];

    url = [NSURL URLWithString:urlString];
    data = [NSData dataWithContentsOfURL:url];
    json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

    NSMutableArray *idWedstrijdDagprogramma = [NSMutableArray array];
    NSMutableArray *naam = [NSMutableArray array];
    NSMutableArray *uur = [NSMutableArray array];

    for(NSDictionary *item in json)
    {
        [idWedstrijdDagprogramma addObject:[item objectForKey:@"id"]];

        [naam addObject:[item objectForKey:@"naam"]];
        [uur addObject:[item objectForKey:@"uur"]];
    }

    DagprogrammaDetail = [NSMutableArray arrayWithObjects: nil];

    for(int i = 0; i < json.count; i ++)
    {
        NSMutableString *lijn =[NSMutableString stringWithFormat:@"%@", [naam objectAtIndex:i]];
        [lijn appendString:[NSMutableString stringWithFormat:@"%s", "\t \t"]];
        [lijn appendString:[NSMutableString stringWithFormat:@"%@", [uur objectAtIndex:i]]];
        [DagprogrammaDetail addObject:lijn];
    }
}

//methode om aantal items te weten van de array
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [DagprogrammaDetail count];
}
//methode om een item aan een index te koppelen en zichtbaar te maken
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"MenuDetailCell";

    UITableViewCell *cell = [self.tableDagprogrammaDetail dequeueReusableCellWithIdentifier:simpleTableIdentifier];

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

    }
    cell.textLabel.numberOfLines = 1;
    cell.textLabel.text = [DagprogrammaDetail objectAtIndex:indexPath.row];

    return cell;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

4 个答案:

答案 0 :(得分:1)

  

我很困惑,知道,所以你知道如何解决它?其他的Cus   查看控制器它工作正常,它是相同的代码和方法。什么   我该怎么办?

  • 冷静下来并重新检查您的代码。我保证这将是一个愚蠢的错误。当你感到沮丧时,任何人都可能会遇到这种情况。

您可以做的事情:

1) NSLog您的numberOfRowsInSection。请注意,如果“0”,则永远不会调用cellForRowAtIndexPath

2)如果您在某些UITableView内使用UIView,则应添加:

[self.view addSubview:table];

3)不要忘记包括:

@interface yourViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>

答案 1 :(得分:1)

我刚刚在ViewDidLoad中改变了一点点并且工作正常

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.title = menuNaam;

    NSArray *dagarray = [menuNaam componentsSeparatedByString:@" "];

    NSString *plaatsString = [dagarray objectAtIndex:1];
    plaatsString = [plaatsString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

    //object ophalen
    NSMutableString *urlString = [NSMutableString stringWithFormat:@"http://zesdaagse.mobi-app.be/WCFUrl/MobileService.svc/GetDagprogramma"];
    NSURL *url = [NSURL URLWithString:urlString];
    NSData *data = [NSData dataWithContentsOfURL:url];
    NSError *error;

    NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

    NSMutableArray *idss = [[NSMutableArray alloc]init];;

    NSLog(@"Json Data =%@",json);
    for(NSDictionary *item in json)
    {
        [idss addObject:[item objectForKey:@"id"]];
    }

    int plaats = [plaatsString intValue];
    plaats -= 1;
    //id van de dag ophalen
    NSString *idDagprogramma = [idss objectAtIndex:0];

    urlString = [NSMutableString stringWithFormat:@"http://zesdaagse.mobi-app.be/WCFUrl/MobileService.svc/GetDagprogrammaWedstrijden?id="];
    [urlString appendString:[NSString stringWithFormat:@"%@",idDagprogramma]];

    url = [NSURL URLWithString:urlString];
    data = [NSData dataWithContentsOfURL:url];
    json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

    NSMutableArray *idWedstrijdDagprogramma = [NSMutableArray array];
    NSMutableArray *naam = [NSMutableArray array];
    NSMutableArray *uur = [NSMutableArray array];

    for(NSDictionary *item in json)
    {
        [idWedstrijdDagprogramma addObject:[item objectForKey:@"id"]];

        [naam addObject:[item objectForKey:@"naam"]];
        [uur addObject:[item objectForKey:@"uur"]];
    }

    DagprogrammaDetail = [[NSMutableArray alloc]init];

    for(int i = 0; i < json.count; i ++)
    {
        NSMutableString *lijn =[NSMutableString stringWithFormat:@"%@", [naam objectAtIndex:i]];
        [lijn appendString:[NSMutableString stringWithFormat:@"%s", "\t \t"]];
        [lijn appendString:[NSMutableString stringWithFormat:@"%@", [uur objectAtIndex:i]]];
        [DagprogrammaDetail addObject:lijn];
    }
}

下载我的项目Here 链接:http://www.4shared.com/zip/FI1HJV1c/DagprogrammaDetail.html

答案 2 :(得分:0)

如果您不发布完整代码,我们无法知道发生了什么。显然你错过了什么。按照您的代码逐步执行此步骤tutorial,以查找问题隐藏的位置。

答案 3 :(得分:0)

我认为您的问题在于UITableViewDataSource协议。有一种方法叫做: - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

您不必实现此方法,因为默认情况下它返回0.因此,如果您实现此方法并且此方法返回0,则您遇到问题。因为您没有任何部分,所以创建行的数据源方法永远不会被调用,因为您的tableview中没有任何部分。

要使其正常工作,要么您没有实施

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

方法,或者必须返回至少1个。