PFQueryTableViewController上的didSelectRowAtIndexPath方法

时间:2015-02-17 20:50:46

标签: ios uitableview parse-platform

我正在使用Parser和iOS创建我的第一个应用程序。现在我只有一个带有Parse对象的tableview,但我无法点击一行并打开一个视图控制器来显示所选对象的详细信息。 这就是我如何从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;
}

这是我的cellForRowAtIndexPath方法:

- (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;
}

这就是didSelectRowAtIndexPath方法:

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


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

我一直在搜索没有成功如何让所选行对象将其传递给详细视图控制器。

欢迎任何帮助。

1 个答案:

答案 0 :(得分:1)

您可以使用数组存储对象并在cellForRowAtIndexPath

中更新它们

例如:(我在这里使用了字典,因为查询结果的计数未定义; OperationPFObject的子类)

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]!)
    }
}
相关问题