2015-12-22 143 views
0

我知道cellForRowAtIndexPath方法没有被调用,因为我从来没有看到其中的NSLog消息。我已经设置了UITableViewdataSourcedelegate属性,并且我还在头文件中声明了UITableViewDelegateUITableViewDataSource。我错过了什么?UITableView cellForRowAtIndexPath没有被调用

- (void)viewDidLoad { 
[self.view setBackgroundColor:[UIColor grayColor]]; 
tableData = [[NSMutableArray alloc] init]; 
matchesForUser = [[NSMutableArray alloc] init]; 
sortedFirstArray = [[NSArray alloc] init]; 
sortedSecondArray = [[NSArray alloc] init]; 

_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height - 20) style:UITableViewStylePlain]; 
_tableView.dataSource = self; 
_tableView.delegate = self; 
[self.view addSubview:_tableView]; 

countUsers = 0; 
PFQuery *query = [PFQuery queryWithClassName:@"_User"]; 
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { 
    if (!error) { 
     for (PFObject *object in objects) { 
      NSLog(@"%@", object.objectId); 
      tableData[countUsers] = [object valueForKey:@"username"]; 
      matchesForUser[countUsers] = [object valueForKey:@"matches"]; 
     } 
    }else{ 
     NSLog([error description]); 
    } 

    NSLog(@"***tabledata***"); 
    NSLog([NSString stringWithFormat:@"%lu", (unsigned long)[tableData count]]); 
    NSLog(@"***matchesdata***"); 
    NSLog([NSString stringWithFormat:@"%lu", (unsigned long)[matchesForUser count]]); 
}]; 

dictionary = [NSDictionary dictionaryWithObjects:matchesForUser forKeys:tableData]; 
sortedFirstArray = [[dictionary allKeys] sortedArrayUsingSelector:@selector(compare:)]; 
sortedSecondArray = [dictionary objectsForKeys:sortedFirstArray notFoundMarker:[NSNull null]]; 

backToMap = [[UIButton alloc] initWithFrame:CGRectMake(80, 518, 160, 30)]; 
backToMap.backgroundColor = [UIColor colorWithRed:33.0f/255.0f green:156.0f/255.0f blue:41.0f/255.0f alpha:1.0f]; 
[backToMap addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside]; 
[backToMap setTitle:@"BACK TO MAP" forState:UIControlStateNormal]; 
[self.view addSubview:backToMap]; 
} 

- (void)dismiss { 
    [self dismissViewControllerAnimated:YES completion:nil]; 
} 

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

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 

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

    NSString *username = [[sortedFirstArray objectAtIndex:indexPath.row] stringByAppendingString:@" "]; 
    NSString *matchAmount = [sortedSecondArray objectAtIndex:indexPath.row]; 

    NSLog(@"***username***"); 
    NSLog(username); 
    NSLog(@"***matchamount***"); 
    NSLog(matchAmount); 

    cell.textLabel.text = [username stringByAppendingString:matchAmount]; 

    return cell; 
} 
+0

你检查你的资料表阵列?是否意味着它包含任何对象?因为我检查你的代码与静态数据,它工作正常。 –

+0

并且是在for循环之后重新加载你的tableview。 –

回答

2

1)在这里你需要使用countUsers = 0;如果你需要比你需要通过1.Otherwise它将覆盖内部递增值循环每次索引0处的值。

2)您使用的背景调用方法以提取data.So它需要一定的时间来获取数据和控制将更进一步,执行以下code.But当时matchesForUser阵列是空的,并且字典是也是empty.So在单元格中它不会显示任何东西,你不会看到细胞的NSLOG。

取而代之的是

INI

dictionary = [NSDictionary dictionaryWithObjects:matchesForUser forKeys:tableData]; 
sortedFirstArray = [[dictionary allKeys] sortedArrayUsingSelector:@selector(compare:)]; 
sortedSecondArray = [dictionary objectsForKeys:sortedFirstArray notFoundMarker:[NSNull null]]; 

3)尝试用这个,

PFQuery *query = [PFQuery queryWithClassName:@"_User"]; 
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     if (!error) { 
      for (PFObject *object in objects) { 
       NSLog(@"%@", object.objectId); 
       [tableData addObject:[object valueForKey:@"username"]]; 
       [matchesForUser addObject:[object valueForKey:@"matches"]]; 
      } 
      dictionary = [NSDictionary dictionaryWithObjects:matchesForUser forKeys:tableData]; 
      sortedFirstArray = [[dictionary allKeys] sortedArrayUsingSelector:@selector(compare:)]; 
      sortedSecondArray = [dictionary objectsForKeys:sortedFirstArray notFoundMarker:[NSNull null]]; 
      [_tableView reloadData]; 

     }else{ 
      NSLog([error description]); 
     } 

     NSLog(@"***tabledata***"); 
     NSLog([NSString stringWithFormat:@"%lu", (unsigned long)[tableData count]]); 
     NSLog(@"***matchesdata***"); 
     NSLog([NSString stringWithFormat:@"%lu", (unsigned long)[matchesForUser count]]); 
    }); 
}]; 
+0

这工作完美!谢谢! – ewizard

+1

欢迎开心编码.. – Bhoomi

1

您是否重新加载ViewDidLoad中的Tableview?像

[_tableView reloadData]; 
5

试着做下面的事情

1)注册您的tableView像

[_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"SimpleTableItem"]; 

2)然后略低于

for (PFObject *object in objects) { 
     NSLog(@"%@", object.objectId); 
     tableData[countUsers] = [object valueForKey:@"username"]; 
     matchesForUser[countUsers] = [object valueForKey:@"matches"]; 
    } 

添加​​内if (!error)一部分。 。

所以你更新的代码必须看起来像

[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { 
if (!error) { 
    for (PFObject *object in objects) { 
     NSLog(@"%@", object.objectId); 
     tableData[countUsers] = [object valueForKey:@"username"]; 
     matchesForUser[countUsers] = [object valueForKey:@"matches"]; 
    } 
    [_tableView reloadData]; 
}else{ 
    NSLog([error description]); 
} 

NSLog(@"***tabledata***"); 
NSLog([NSString stringWithFormat:@"%lu", (unsigned long)[tableData count]]); 
NSLog(@"***matchesdata***"); 
NSLog([NSString stringWithFormat:@"%lu", (unsigned long)[matchesForUser count]]); 
}]; 
+0

现在一切正常 - 为什么我需要注册我的tableview? – ewizard

+1

注册tableview将自动生成该特定单元标识符的视图。这将有助于快速ui更新,当你将调用cellForRowAtIndex –

+0

很酷的感谢信息! – ewizard

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

count为0?您可以输入NSLog("%@", @(tableData.count));或仅需return 1;进行测试。 UITableView先发送tableView:numberOfRowsInSection:,如果方法返回0.它不会调用cellForRowAtIndexPath

而@PiyushSharma提到,你应该Register your tableView

+1

'findObjectsInBackgroundWithBlock' add'[tableview reloadData]'如果有**没有错误**。 –

1

刷新表后再次

dictionary = [NSDictionary dictionaryWithObjects:matchesForUser forKeys:tableData]; 
sortedFirstArray = [[dictionary allKeys] sortedArrayUsingSelector:@selector(compare:)]; 
sortedSecondArray = [dictionary objectsForKeys:sortedFirstArray notFoundMarker:[NSNull null]]; 

[_tableView reloadData]; 
相关问题