Note that there are some explanatory texts on larger screens.

plurals
  1. POCan't add cell to UITableView from Modal VC to Table VC (complicated)
    text
    copied!<p>I'm trying to add a cell to my table view controller from a modal view controller. My data source array has multiple properties (name, time interval). Right now I have a delegate/protocol in my modal view controller that sends the data to the table view controller. However, for some reason I can add the data to the array but I can't add a cell to the tableview with that data. Here is my code:</p> <p>ToDoTableViewController.h (TableViewController)</p> <pre><code> @interface ToDoTableViewController : UITableViewController &lt;UITableViewDataSource, Properties2ViewControllerDelegate&gt; { IBOutlet UIView *headerView; } @property (strong, nonatomic) NSMutableArray *taskArray; -(UIView *)headerView; -(IBAction)addCell:(id)sender; </code></pre> <p>ToDoTableViewController.m (TableViewController)</p> <pre><code>-(void) viewDidLoad{ [[self tableView] setDataSource:self]; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"]; if (!cell) cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"]; [[cell textLabel] setText:[taskArray objectAtIndex:[indexPath row]]]; return cell; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return [taskArray count]; } -(IBAction)addCell:(id)sender{ Properties2ViewController *pvc = [[Properties2ViewController alloc]init]; [pvc setDelegate:self]; [self presentViewController:pvc animated:YES completion:NULL]; } -(UIView *)headerView{ if (!headerView){ [[NSBundle mainBundle] loadNibNamed:@"HeaderView" owner:self options:nil]; } return headerView; } -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ return [self headerView]; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ return [[self headerView] bounds].size.height; } -(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; [[self tableView] reloadData]; } -(void)properties2ViewControllerDidEnterPropertiesSuccesfully:(Tasks *)t{ [taskArray addObject:t]; } </code></pre> <p>Properties2ViewController.h (ModalViewController)</p> <pre><code> @class Tasks; @protocol Properties2ViewControllerDelegate; @interface Properties2ViewController : UIViewController &lt;UITextFieldDelegate&gt;{ __weak IBOutlet UITextField *taskName; __weak IBOutlet UIButton *doneButton; __weak IBOutlet UIDatePicker *datePicker; __weak IBOutlet UILabel *label1; __weak IBOutlet UILabel *label2; } @property (strong, nonatomic) Tasks *testTask; @property (weak, nonatomic) id &lt;Properties2ViewControllerDelegate&gt; delegate; -(IBAction)dismiss:(id)sender; -(IBAction)cancel:(id)sender; @end @protocol Properties2ViewControllerDelegate &lt;NSObject&gt; @optional -(void)properties2ViewControllerDidEnterPropertiesSuccesfully:(Tasks *)t; @end </code></pre> <p>Properties2ViewController.m (ModalViewController)</p> <pre><code>-(IBAction)dismiss:(id)sender{ testTask = [[Tasks alloc]initWith:[taskName text] :[datePicker countDownDuration] :[NSDate date]]; if ([self.delegate respondsToSelector:@selector (properties2ViewControllerDidEnterPropertiesSuccesfully:)]){ [self.delegate properties2ViewControllerDidEnterPropertiesSuccesfully:testTask]; } [self dismissViewControllerAnimated:YES completion:NULL]; } -(void)viewWillDisappear:(BOOL)animated{ [super viewWillDisappear:animated]; } -(BOOL) textFieldShouldReturn:(UITextField *)aTextField{ if (aTextField.tag == 1){ [taskName resignFirstResponder]; } return YES; } -(void)viewDidDisappear:(BOOL)animated{ [super viewDidDisappear:animated]; } -(IBAction)cancel:(id)sender{ [self dismissViewControllerAnimated:YES completion:NULL]; } @end </code></pre> <p>Here are the properties of the Task class...if it helps at all...</p> <pre><code>@interface Tasks : NSObject @property (strong, nonatomic) NSString *taskName; @property NSTimeInterval timeInterval; @property NSDate *dateCreated; -(id)initWith:(NSString *)tskNme :(NSTimeInterval)timeInt :(NSDate *)dateCreat; @end </code></pre> <p>---EDIT----- So I tried putting this in my properties2ViewControllerDidEnterPropertiesSuccesfully delegate method but the cell is still not being created...:</p> <pre><code>-(void)properties2ViewControllerDidEnterPropertiesSuccesfully:(Tasks *)t{ [taskArray addObject:t]; [self.tableView reloadData]; int lastRow = [[self tableView] numberOfRowsInSection:0]; NSIndexPath *ip = [NSIndexPath indexPathForItem:lastRow inSection:0]; [self.tableView cellForRowAtIndexPath:ip]; } </code></pre> <p>Also, if i switch </p> <pre><code>[self.tableView cellForRowAtIndexPath:ip]; </code></pre> <p>with</p> <pre><code>[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:ip] withRowAnimation:UITableViewRowAnimationTop]; </code></pre> <p>then an exception is thrown (Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update')</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload