Ios 行始终为零(为什么不工作?)
提示,这是一个按距离对字典排序的代码,我需要将一个变量coord传递到detailview,在detailview中,它被分离到相应的位置,并将标记放在karte上。不,为什么coord给出0,以及如何将其传递到坐标Ios 行始终为零(为什么不工作?),ios,objective-c,xcode,Ios,Objective C,Xcode,提示,这是一个按距离对字典排序的代码,我需要将一个变量coord传递到detailview,在detailview中,它被分离到相应的位置,并将标记放在karte上。不,为什么coord给出0,以及如何将其传递到坐标 - (void)viewDidLoad {[self setupArray]; [super viewDidLoad];} -(void)setupArray{ self.myLocationManager = [[CLLocationManager alloc
- (void)viewDidLoad
{[self setupArray];
[super viewDidLoad];}
-(void)setupArray{ self.myLocationManager = [[CLLocationManager alloc] init];
[states setObject: @12 forKey:@"60.050043,30.345783"];
[states setObject: @11 forKey:@"60.037389,30.322094"];
[states setObject: @32 forKey:@"60.037329,30.322014"];
[states setObject: @1 forKey:@"59.957387,30.324681"];
NSLog(@"%f",betweenDistance);
NSArray* sortedStates = [states keysSortedByValueUsingComparator: ^(id obj1, id obj2)
{
if ([obj1 floatValue] > [obj2 floatValue])
{
return (NSComparisonResult)NSOrderedDescending;
}
if ([obj1 floatValue] < [obj2 floatValue])
{
return (NSComparisonResult)NSOrderedAscending;
}
return (NSComparisonResult)NSOrderedSame;
}];
NSLog(@"%@", sortedStates);
NSMutableArray* rows = [[NSMutableArray alloc] init];
for (NSString* key in sortedStates)
{
CGFloat distance = [[states objectForKey:key] floatValue];
[rows addObject:[NSString stringWithFormat:@"Distance is %f km", distance]];
}
datasource=rows;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 4;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Detail2ViewController *detail = [self.storyboard instantiateViewControllerWithIdentifier:@"detail"];
[self.navigationController pushViewController:detail animated:YES];
NSString* coord = sortedStates[indexPath.row];
NSLog(@"virable coord %@",coord);
}
更新2
我使用这个功能,可能是因为它不工作
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
// Configure the cell...
//---------- CELL BACKGROUND IMAGE -----------------------------
UIImageView *imageView = [[UIImageView alloc] initWithFrame:cell.frame];
UIImage *image = [UIImage imageNamed:@"LightGrey.png"];
imageView.image = image;
cell.backgroundView = imageView;
[[cell textLabel] setBackgroundColor:[UIColor clearColor]];
[[cell detailTextLabel] setBackgroundColor:[UIColor clearColor]];
cell.textLabel.text = [datasource objectAtIndex:indexPath.row];
//Arrow
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
这是因为sortedStates[indexPath.row];将生成一本词典。 所以代码应该是:
NSDictionary *coord = sortedStates[indexPath.row];
将此值传递给detailViewController并提取坐标。在
[super viewDidLoad]
之前调用[self setupArray]
不是一个好的做法。以前打过电话
在-(void)setupArray
上加载数据后,尝试调用UITableView的方法-(void)reloadData
,告知tableView的代理您已加载或更新数据,并希望在tableView上显示。它应该能解决你的问题
仅供参考:在您的情况下,numberOfRowsInSection:
应返回[sortedStates count]
,以确保当您点击手机时,您的应用不会崩溃
更新
- (void)viewDidLoad
{
[super viewDidLoad];
//Call setup array after [super viewDidLoad]
[self setupArray];
}
-(void)setupArray {
//Do all your setupArray stuff
//Now update the table contents
[yourTableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//Use [sortedStates count] here to be sure your app will not crash later
return [sortedStates count];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Detail2ViewController *detail = [self.storyboard instantiateViewControllerWithIdentifier:@"detail"];
[self.navigationController pushViewController:detail animated:YES];
NSString* coord = sortedStates[indexPath.row];
NSLog(@"virable coord %@",coord);
}
更新2
我想我找到了原因!您有一个名为sortedStates的实例变量,但在-(void)setupArray
中,您声明了一个名为的局部变量,这就是为什么您无法在其他方法中获取值的原因
试着改变
NSArray* sortedStates = [states keysSortedByValueUsingComparator: ^(id obj1, id obj2) {
//stuff
}];
与
不幸的是,这样做不起作用:NSDictionary*coord=_sortedStates[indexPath.row];NSLog(@“as%@”,协调);在-(void)tableView中:您在NSLog中得到了什么。请在这里邮寄。也可以在didSelectRowAtIndexPath中打印您的分类状态。请仅在didSelectRowAtIndexPath方法中打印值,我将从中获取IDE。如果您在那里获得值,则意味着您无法将其传递给detailController。如果是这种情况,请告诉我,我将更新我的答案。我不确定我是否正确理解您的意思,但是是
NSDictionary*coord=sortedStates[indexPath.row]并输出null。为了确保我们彼此理解,我把我的文件放在这里(QTableViewController)[link]我想你错了。。。sortedStates不是字典数组,而是包含键的NSString数组。(本例中的坐标…)对不起,但我不明白,我需要在-(void)setupArray
之后插入一个-(void)reloadData
,如下:`-(void)reloadData{…}-(void)setupArray{my code}`但是我需要在方法-(void)reloadData{/code>中插入什么,在执行setupArray之后,需要调用UITableView的reloadData方法。。。我会更新我的答案我会这样做[self.tableView reloadData];但是单元格是空的,我发现这行返回的原因是[sortedStates count];当我返回“返回4;”虚拟坐标为(空)我再次更新了我的答案。。。请返回[sortedStates count];如果可能的话。。。以后你会感谢我的…是的!作品非常感谢你!
NSArray* sortedStates = [states keysSortedByValueUsingComparator: ^(id obj1, id obj2) {
//stuff
}];
//without "NSArray *"
sortedStates = [states keysSortedByValueUsingComparator: ^(id obj1, id obj2) {
//stuff
}];