How to insert a cell in a UITableViewController

I am creating an iPad app. The root UITableview has a right-click panel element in the navigation controller. When you press the button, appears on the display. Popover is a UITableViewController. When you click a cell in a popover, how can I transfer data in that cell and insert it into the cell in the root UITableview? I searched for Apple docs and couldn't find what I needed. Can someone push me in the right direction?

Roottable.h

@interface Roottable : UITableViewController<PopoverDelegate> 

Popover.h

  @protocol AthleteSelectPopoverDelegate <NSObject> @required -(void)selectedObject:(Object *)newObject; @end @property (nonatomic, weak) id<PopoverDelegate> delegate; @property (readwrite, nonatomic) Object *currentObject; @end 

popover.m

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { _currentObject = [_objectArray objectAtIndex:indexPath.row]; //Notify the delegate if it exists. if (_delegate != nil) { [_delegate selectedObject:_currentObject]; } } 
+4
source share
4 answers

I understood that. Hope I help someone. I will explain the code first, and then post it below. Basically, I set the data source as the root table "ObjectSelect", as an NSMutableArray called "currentObjectArray". ObjectSelect is also an ObjectSelectPopoverDelegate. Basically, when a cell in popover is used, it adds the object tapped to "currentObjectArray" and reloads the table.

ObjectSelect.h

 #import <UIKit/UIKit.h> #import "ObjectSelectPopover.h" @interface ObjectSelect : UITableViewController<ObjectSelectPopoverDelegate> @property (nonatomic, strong) ObjectSelectPopover *objectPicker; @property (nonatomic, strong) UIPopoverController *objectPickerPopover; @property (readwrite, nonatomic) Object *currentObject; @property (nonatomic, strong) NSMutableArray *selectedObjectArray; @end 

ObjectSelect.m

 -(void)selectedObject:(Object *)newObject { _currentObject = newObject; if(!_selectedObjectArray){ _selectedObjectArray = [[NSMutableArray alloc] init]; } if([_selectedObjectArray containsObject:_currentAthlete]){ //lol you don't get added, bub } else{ [_selectedObjectArray addObject:_currentObject]; } [self.tableView reloadData]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; Object *objectTapped = (Object *)[_objectAthleteArray objectAtIndex:indexPath.row]; return cell; } 

ObjectSelectPopover.h

 #import <UIKit/UIKit.h> #import "Object.h" @protocol ObjectSelectPopoverDelegate <NSObject> @required -(void)selectedObject:(Object *)newObject; @end @interface ObjectSelectPopover : UITableViewController @property (nonatomic, weak) id<ObjectSelectPopoverDelegate> delegate; @property (nonatomic, strong) NSMutableArray *objectArray; @property (readwrite, nonatomic) Object *currentObject; @end 

ObjectSelectPopover.m

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { _currentObject = [_objectArray objectAtIndex:indexPath.row]; //Notify the delegate if it exists. if (_delegate != nil) { [_delegate selectedObject:_currentObject]; } [self.tableView deselectRowAtIndexPath:indexPath animated:YES]; } 
+1
source

You add data from the selected cell to the data source delegate of the main table.

Then this data source should indicate to the main table that the cell was inserted along the pointer path.

+1
source

I think you should have a property with a name different from the delegate in your popover controller, since the UITableViewController already has a delegate property for the UITableViewDelegate protocol; maybe masterTable or something like that.

Then, in the implementation of selectedObject: in the root UITableView, you can make an insert row or add it to the data array and reload table.

Oops, my bad ... @geraldWilliam is right, the UITableViewController does not have the delegation property ...

What you think should work ... So how is the selectedObject: method called in the delegate? If so, what are you doing in this method? If you add an object to a dataset (array or dictionary or database) for the root view, insert a row in its table (or reload the data), it should work.

Here is the code that works for me. This is not from a popover, but from a pushed glance, but there is no reason that should matter:

 - (ThingStatus) thingPicker: (ThingPickerTableViewController *) thingPicker didSelectThing: (Thing *) thing { NSLog( @"Entering %s", __func__ ); // Dismiss the pushed view controller (for you, the popover) [self.navigationController popViewControllerAnimated: YES]; NSArray *startingList = self.currentCellObjectList; [self.databaseManager addThing: thing]; NSArray *endingList = self.databaseManager.thingsForTableView; // Figure out the differences adding made... DiffResult *changes = [startingList simpleDiffWithArray: endingList]; NSLog( @"%d deletions, %d insertions", changes.deletionCount, changes.insertionCount ); // I only handle insertions in this code... deletions would be similar __block NSUInteger objIdx = 0; NSMutableArray *changeableThingList = [startingList mutableCopy]; [changes.insertionIndexes enumerateIndexesUsingBlock: ^( NSUInteger idx, BOOL *stop ) { NSLog( @" - insert %@ at %d", [[changes.insertionObjects objectAtIndex: objIdx] name], idx ); NSIndexPath *indexPath = [NSIndexPath indexPathForRow: idx inSection: 0]; [changeableThingList insertObject: [changes.insertionObjects objectAtIndex: objIdx] atIndex: idx]; self.currentCellObjectList = changeableThingList; [self.tableView insertRowsAtIndexPaths: [NSArray arrayWithObject: indexPath] withRowAnimation: UITableViewRowAnimationRight]; ++objIdx; }]; [self.databaseManager save]; return [self.databaseManager: thingStatus]; 

}

0
source

Here is some good code to use that might help you.

  - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. return self.item.count; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; } //Get the row Sport *rowSport = self.sports[indexPath.row]; cell.textLabel.text = rowItem.itemName; cell.detailTextLabel.text = rowItem.section; return cell; } 

Hope this helps you.

-2
source

Source: https://habr.com/ru/post/1498690/


All Articles