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中的对象一起填充?我该如何解决?
请显示完整的代码。不要显示代码段。 –
尝试'[tableview reloadData]'当数组已被填充 - 可能是你的数组填充到末尾 – Daivest