2017-02-13 112 views
0

當我運行我的應用程序時,UITableViewCells是空白的。我試圖用「NSArray sortedArray」填充內容,該內容按降序列出日期。 (無符號長整型)[排序陣列數],索引);(2)NSLog(@「sorted array is%@ and count is%lu while string as first index is%@」,sortedArray,(unsigned long)[sortedArray count],indexed);UITableViewCells不會從我的NSArray填充

的NSLog OUTPUT:

2017年2月13日09:50:15.693試驗[22349:7300081]排序陣列( 「2017年2月11日」, 「2017年1月25日」, 「01/17/2017」, 「2016/12/25」, 「2016/12/17」, 「2016/11/25」, 「11/23/2016」, 「11/17/2016年10月「, 」2016/10/11「, 」2016/06/10「, 」2016/08/08「, 」2016/05/08「, 」2016/01/08「, 」 07/28/2016「, 「07/15/2016」, 「07/13/2016」, 「07/11/2016」, 「07/08/2016」, 「2016/6/17」, 「05/17/2016" , 「2016年2月18日」, 「2016年1月18日」, 「2015年12月18日」, 「2015年11月18日」, 「2015年10月18日」, 「09/18/2015」, 「08/18/2015」, 「07/18/2015」, 「06/18/2015」, 「05/22/2015」, 「04/17/2015「, 」03/17/2015「, 」02/17/2015「, 」01/17/2015「 ),計數爲34,而字符串在第一個索引i s 02/11/2017

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 

NSString *strSessionID = [[NSUserDefaults standardUserDefaults] 
          stringForKey:@"sessionID"]; 

NSString *strURL = [NSString stringWithFormat:@"http://%@/get_statement_list?session_id=%@", 
        [[NSUserDefaults standardUserDefaults]stringForKey:@"serverName"], strSessionID]; 


NSURL *aURL = [NSURL URLWithString:strURL]; 
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:aURL]; 
NSURLResponse *response = nil; 
NSError *error = nil; 
NSData *data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error]; 
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil]; NSArray *jsonArray = [dict objectForKey:@"statements"]; 
NSMutableArray *pdfs = [[NSMutableArray alloc] init]; 
NSArray *sortedArray = [[NSArray alloc] init]; 

for (int i=0; i<[jsonArray count]; i++) { 
    NSDictionary* dates = [jsonArray objectAtIndex:i]; 
    NSString *MM=[dates objectForKey:@"month"]; 
    NSString *DD=[dates objectForKey:@"day"]; 
    NSString *YY=[dates objectForKey:@"year"]; 
    NSString *iDates = [NSString stringWithFormat: @"%@/%@/%@", MM, DD, YY]; 
    [pdfs addObject:iDates]; 
} 

//put in descending date order 
NSDateFormatter *df = [[NSDateFormatter alloc] init]; 
[df setDateFormat:@"MM/dd/yyyy"]; 
sortedArray = [pdfs sortedArrayUsingComparator:^NSComparisonResult(NSString 
*obj1, NSString *obj2) { 
    NSDate *d1 = [df dateFromString: obj1]; 
    NSDate *d2 = [df dateFromString: obj2]; 
    return [d2 compare: d1]; 
}]; 

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 

return 1; 
} 

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

} 

- (UITableViewCell *)tableView:(UITableView *)tableView 
cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

UITableViewCell *cell = [tableView 
dequeueReusableCellWithIdentifier:@"MyIdentifier"]; 
if (cell == nil) { 
    cell = [[UITableViewCell alloc] 
initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MyIdentifier"]; 
} 

NSString *statements = [NSString stringWithFormat:@"%@", 
[sortedArray objectAtIndex:indexPath.row]]; 


cell.textLabel.text = statements; 

return cell; 
} 

爲什麼我的tableview單元格不會與sortedArray中的對象一起填充?我該如何解決?

+0

請顯示完整的代碼。不要顯示代碼段。 –

+3

嘗試'[tableview reloadData]'當數組已被填充 - 可能是你的數組填充到末尾 – Daivest

回答

1

如果您已經在Interface Builder中設置了tableview,那麼您是否已將數據源和委託連接到此類。如圖所示enter image description here

用表格視圖中的ctrl按鈕拖動到文件的所有者。 enter image description here

如果您以編程方式提供了tableview,那麼您必須在代碼中進行連接。

+0

是的,我已經在接口構建器中連接了委託和數據源。 :/ – Kelly

+0

嘗試在cellForRowAtIndexPath:方法上設置斷點,以檢查方法是否被調用。如果沒有,也許委託協議UITableViewDelegate,UITableViewDataSource丟失。 – voltae

+1

我的問題解決了。使該表成爲屬性而不是實例變量。通過「self.sortedArray」引用NSArray,並最終[tableview reloadData]工作!謝謝! @Daivest – Kelly