2015-12-22 141 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]; 
相關問題