如何在PFQuery TableView编辑模式中添加新的插入行?

时间:2013-10-24 02:15:10

标签: ios objective-c uitableview parse-platform

我想知道如何在PFQueryTableView中添加新的插入行。我的表视图运行良好,可以正确加载所有PFObject。但是,我想在表视图底部添加一个新行,这样当我点击它时,它会弹出另一个视图控制器来创建一个新的PFObject。由于PFQueryTableViewController带有Edit Button,只允许删除PFObject。你能救我一下吗?

-viewDidLoad

self.navigationItem.rightBarButtonItem = self.editButtonItem;

-tableView:numberOfRowsInSection:

return self.tableView.isEditing ? self.objects.count + 1 : self.objects.count;

-tableView:cellForRowAtIndexPath:object:

BOOL isInsertCell = (indexPath.row == self.objects.count && tableView.isEditing);
NSString *CellIdentifier = @"CustomCell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
    cell = [topLevelObjects objectAtIndex:0];
}
// Configure the cell
UILabel *cellLocationLabel = (UILabel *)[cell.contentView viewWithTag:100];
cellLocationLabel.text = isInsertCell ? @"Add a new location" : [object objectForKey:@"address"];
return cell;

1 个答案:

答案 0 :(得分:0)

按照您描述的方式执行此操作的问题是没有相应的PFObject传递到tableView:cellForRowAtIndexPath:object:方法。这可能会导致问题。此外,用户必须滚动到底部才能访问添加按钮。

更好的方法(以及我执行此操作的方式,因为我的应用程序执行此操作)将只是向导航栏添加另一个按钮。

viewDidLoad或您的自定义init方法中:

// Make a new "+" button
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addButtonPressed)];
NSArray *barButtons = [NSArray arrayWithObjects:self.editButtonItem,addButton,nil];
self.navigationItem.rightBarButtonItems = barButtons;

然后在addButtonPressed方法中:

// The user pressed the add button
MyCustomController *controller = [[MyCustomController alloc] init];
[self.navigationController pushViewController:controller animated:YES];
// Replace this with your view controller that handles PFObject creation

如果您只希望用户能够在编辑模式下创建新对象,请将逻辑移至setEditing:animated:方法:

- (void) setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    if(editing)
    {
        UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addButtonPressed)];
        // self.editButtonItem turns into a "Done" button automatically, so keep it there
        NSArray *barButtons = [NSArray arrayWithObjects:self.editButtonItem,addButton,nil];
        self.navigationItem.rightBarButtonItems = barButtons;
    }
    else
        self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

希望有所帮助!这就是我这样做的方式(在某种程度上),在我看来比在tableView底部的单元格内部有一个按钮更清晰。