PFQueryTableViewController的didSelectRowAtIndexPath方法 [英] didSelectRowAtIndexPath method at PFQueryTableViewController

查看:79
本文介绍了PFQueryTableViewController的didSelectRowAtIndexPath方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Parser和iOS创建我的第一个应用程序.现在我只有一个带有Parse对象的表格视图,但是我无法点击一行并打开视图控制器来显示所选对象的详细信息. 这就是我从Parse获取对象的方式:

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

这是我的cellForRowAtIndexPath方法:

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

这是didSelectRowAtIndexPath方法:

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.

欢迎任何帮助.

推荐答案

您可以使用数组存储对象并在cellForRowAtIndexPath中更新它们

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

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

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]!)
    }
}

这篇关于PFQueryTableViewController的didSelectRowAtIndexPath方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆