0

我想知道如何在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;
4

1 回答 1

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 底部的单元格内有一个按钮更干净。

于 2013-10-26T21:42:18.290 回答