UITableViewCells不会从我的NSArray填充

问题描述:

当我运行我的应用程序时,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

如果您已经在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