我有一个PFQueryTableViewController,我加载了某些信息。我想获取您单击的单元格的值以打开另一个表,其中包含您单击的单元格的类名称的信息。我尝试了很多方法,但它没有工作,我认为我没有采用单元格标签的值,因为错误说:
由于未捕获的异常终止应用' NSInvalidArgumentException',原因:' setObjectForKey:object不能是nil(key:classname)'
我正在使用此代码:
- (id)initWithCoder:(NSCoder *)aCoder {
self = [super initWithCoder:aCoder];
if (self) {
// Custom the table
// The className to query on
self.parseClassName = @"Hospitales";
// The key of the PFObject to display in the label of the default cell style
self.textKey = @"Hospital";
// The title for this table in the Navigation Controller.
self.title = @"Hospitals";
// Whether the built-in pull-to-refresh is enabled
self.pullToRefreshEnabled = YES;
// Whether the built-in pagination is enabled
self.paginationEnabled = YES;
// The number of objects to show per page
self.objectsPerPage = 10;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (PFQuery *)queryForTable {
PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];
// If no objects are loaded in memory, we look to the cache first to fill the table
// and then subsequently do a query against the network.
if ([self.objects count] == 0) {
query.cachePolicy = kPFCachePolicyCacheThenNetwork;
}
[query orderByAscending:@"Location"];
return query;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
// Configure the cell
cell.textLabel.text = [object objectForKey:@"Hospital"];
cell.detailTextLabel.text = [NSString stringWithFormat:@"Location: %@", [object objectForKey:@"Location"]];
return cell;
}
-(void)tableView:(UITableView *)tableView CellAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
cell.textLabel.text = [_data objectAtIndex:indexPath.row];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:@"People" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Check that a new transition has been requested to the DetailViewController and prepares for it
if ([segue.identifier isEqualToString:@"People"]){
//Get the selected data
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSString* hospitalData = _data[indexPath.row];
[[segue destinationViewController]setHospital:hospitalData];
}
}
答案 0 :(得分:0)
依靠用户界面获取所需数据并不安全,因此您违反了核心MVC概念。
<强> didSelectRowAtIndexPath方法强>
-(void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[self performSegueWithIdentifier:@"People" sender:self];
}
<强> prepareForSegue 强>
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"People"]){
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
PFObject *object = [self.objects objectAtIndex:indexPath.row];
[[segue destinationViewController] setDetailItem:object];
}
}