2013-10-24 54 views
0

我想知道如何在PFQueryTableView添加新插入行。我的表格视图运行良好,可以正确加载所有PFObjects。不过,我想在表视图底部添加新行,这样当我点击,它会弹出另一个视图控制器来创建一个新PFObject。作为PFQueryTableViewController带有其Edit Button这是只允许删除PFObject。你能帮我吗?如何在PFQueryTableView编辑模式下添加新插入行?

-viewDidLoad

self.navigationItem.rightBarButtonItem = self.editButtonItem; 

-tableView:numberOfRowsInSection:

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

-tableView:的cellForRowAtIndexPath:对象:

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; 

回答

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

相关问题