2013-10-24 2 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; 
} 

Надеюсь, что это поможет! Так я это делаю (вроде) и, на мой взгляд, немного чище, чем с кнопкой внутри ячейки в нижней части таблицыView.

Смежные вопросы