Objective c UITableViewCells将不会从我的NSArray中填充
运行应用程序时,UITableViewCells为空。我试图用NSArray SorterDarray的内容来填充它们,它按降序列出日期 NSLog@sorted array为%@且计数为%lu,而第一个索引处的字符串为%@,sortedArray,无符号长[sortedArray count],已索引 NSLOG输出: 2017-02-13 09:50:15.693测试[22349:7300081]排序的数组是 02/11/2017, 01/25/2017, 01/17/2017, 12/25/2016, 12/17/2016, 11/25/2016, 11/23/2016, 11/17/2016, 10/11/2016, 10/06/2016, 08/08/2016, 08/05/2016, 08/01/2016, 07/28/2016, 07/15/2016, 07/13/2016, 07/11/2016, 07/08/2016, 06/17/2016, 05/17/2016, 02/18/2016, 01/18/2016, 12/18/2015, 11/18/2015, 10/18/2015, 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,而第一个索引的字符串为2017年11月2日Objective c UITableViewCells将不会从我的NSArray中填充,objective-c,uitableview,Objective C,Uitableview,运行应用程序时,UITableViewCells为空。我试图用NSArray SorterDarray的内容来填充它们,它按降序列出日期 NSLog@sorted array为%@且计数为%lu,而第一个索引处的字符串为%@,sortedArray,无符号长[sortedArray count],已索引 NSLOG输出: 2017-02-13 09:50:15.693测试[22349:7300081]排序的数组是 02/11/2017, 01/25/2017, 01/17/2017, 12/25
- (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单元格不能填充Darray中的对象?如何修复此问题?如果已在Interface Builder中设置tableview,是否已将数据源和委托连接到此类。如图所示 按住ctrl键从表视图拖动到文件的所有者
如果您以编程方式提供了tableview,则必须在代码中建立连接。请显示完整的代码。不显示代码片段。请在array allready填充时尝试[tableview reloadData]-可能是您的数组填充到Late是的,我已经在interface builder中连接了代理和数据源:/如果调用了方法,请尝试在cellForRowAtIndexPath:方法上设置要检查的断点。如果没有,则可能缺少代理协议UITableViewDelegate,UITableViewDataSource。我的问题已解决。使表成为属性而不是实例变量。self.SorterDarray引用了NSArray,最终[tableview reloadData]成功了!谢谢@戴维斯特