2

I am creating my first app using Parser with iOS. Now I have just a tableview with Parse objects, but I am not able to tap on a row and open a view controller to show the selected object details. This is how am I getting the objects from Parse:

- (id)initWithCoder:(NSCoder *)aCoder {
    self = [super initWithCoder:aCoder];
    if (self) {
        // Customize the table

        // The className to query on
        self.parseClassName = @"cadenas";

        // The key of the PFObject to display in the label of the default cell style
        self.textKey = @"chain_name";

        // Uncomment the following line to specify the key of a PFFile on the PFObject to display in the imageView of the default cell style
        // self.imageKey = @"image";

        // 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 = 25;
    }
    return self;
}

- (PFQuery *)queryForTable {
    PFQuery *query = [PFQuery queryWithClassName:@"cadenas"];


    if ([self.objects count] == 0) {
        query.cachePolicy = kPFCachePolicyCacheThenNetwork;
    }

    [query orderByAscending:@"createdAt"];

    return query;
}

This is my cellForRowAtIndexPath method:

- (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 to show todo item with a priority at the bottom
    cell.textLabel.text = [object objectForKey:@"chain_name"];

    return cell;
}

And this the didSelectRowAtIndexPath method:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Detalle_ChainViewController *detailViewController =[self.storyboard instantiateViewControllerWithIdentifier:@"detalle_chain"];


    NSLog(@"CELL TAPPED");
    [self.navigationController pushViewController:detailViewController animated:YES];
}

I have been searching without success how to get the selected row object to pass it to the detail view controller.

Any help is welcome.

mvasco
  • 4,965
  • 7
  • 59
  • 120
  • Do you see the "CELL TAPPED" message? – Phillip Mills Feb 17 '15 at 20:53
  • @PhillipMills, yes I do.... – mvasco Feb 17 '15 at 20:54
  • 1
    Check to see that both `detailViewController` and `self.navigationController` are valid (non-nil) objects. – Phillip Mills Feb 17 '15 at 20:56
  • @PhillipMills, detailViewController is valid, self.navigationController is (null). – mvasco Feb 17 '15 at 21:01
  • @PhillipMills, you are right, my problem is to get the object information to pass it to the detail controller, but now I assume that the navigation controller is also wrong. – mvasco Feb 17 '15 at 21:02
  • 1
    Yes, if your current view controller isn't part of an on-screen navigation stack, "push" isn't going to work for you. (Or, more accurately, if you can't get a valid navigation controller that's handling the screen.) – Phillip Mills Feb 17 '15 at 21:03
  • @PhillipMills, yes, but this is then a secondary issue that I have to solve later, what I need now is know how to get the selected object information to pass it to the detail controller. – mvasco Feb 17 '15 at 21:06
  • @PhillipMills, I have solved the navigation issue, I have change it to this: [self performSegueWithIdentifier:@"edit_segue" sender:tableView]; – mvasco Feb 17 '15 at 21:20

1 Answers1

1

You can use an array to store your objects and update them in cellForRowAtIndexPath

For example: (I used dictionary here, because count of query results was undefined; Operation is subclass of PFObject)

class HistoryViewController: PFQueryTableViewController {
    var operations: [Int: Operation] = [:]

    override init!(style: UITableViewStyle, className: String!) {
        super.init(style: style, className: className)
    }

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        parseClassName = "Operation";
        textKey = "title";
        pullToRefreshEnabled = true;
        paginationEnabled = true;
        objectsPerPage = 25;
    }

    override func queryForTable() -> PFQuery! {
        var query = Operation.query()
        query.whereKey("wallet", equalTo: wallet)
        query.addDescendingOrder("date")
        return query
    }    

    override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!, object: PFObject!) -> PFTableViewCell! {
        let operation = object as! Operation
        operations[indexPath.row] = operation
        var cell: PFTableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as! PFTableViewCell

        // set up your cell

        return cell
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        let row = tableView.indexPathForSelectedRow()!.row
        (segue.destinationViewController as! OperationInfoViewController).loadOperation(operations[row]!)
    }
}
egor.zhdan
  • 4,555
  • 6
  • 39
  • 53
  • Thank you egor, but how will it help me ? – mvasco Feb 17 '15 at 20:57
  • 1
    In cellForRowAtIndexPath you can put your PFObject to an array (you know indexPath of cell, so it's possible to get row index), then in didSelectRowAtIndexPath get PFObject from array by index of row. – egor.zhdan Feb 17 '15 at 21:01
  • Understood,but I guess that there is another way to get the object information directly at the didSelectRowAtIndexPath method...am I wrong? – mvasco Feb 17 '15 at 21:04
  • I also had this problem, and I didn't find any information about this in Parse documentation. I think this is not implemented yet. – egor.zhdan Feb 17 '15 at 21:09
  • I didn't find any information either, may be this time, someone can help us. But could you show me the code you wrote to get it working as you propose? – mvasco Feb 17 '15 at 21:10
  • This isn't working for me. I can't select a cell in order for it to lead to another viewController... Any ideas? `Operator` is undefined in my project and isn't a subclass. – Lukesivi Nov 10 '15 at 13:50