1

I have successfully set up a UITableView that pulls its cell labels from an array. What I am trying to do is add an object to that array and reload the UITableView so it'll show that new object and create a new cell. Unfortunately, I have no luck.

I know it is probably something very stupid that I did in the code, but can someone let me know where I went wrong? Here is what I am currently using:

here is my whole .h:

@interface crastinatrViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> {

NSMutableArray *tasksList;

UIView *addtaskview;

}

@property (strong, nonatomic) IBOutlet UITableView *mainTableView;

@property (nonatomic, retain) NSMutableArray *tasksList;

@property (strong, nonatomic) IBOutlet UITextField *taskName;

- (IBAction)addTask:(id)sender;
- (IBAction)startaddTask:(id)sender;
@end

and my whole .m:

@interface crastinatrViewController ()

@end

@implementation crastinatrViewController

@synthesize mainTableView;

@synthesize tasksList;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    tasksList = [[NSMutableArray alloc] initWithObjects:nil];
    self.mainTableView.delegate = self;
    self.mainTableView.dataSource = self;
    [self.taskName becomeFirstResponder];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    NSInteger rows = [[self tasksList] count];

    NSLog(@"rows is: %d", rows);
    return rows;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSString *contentForThisRow = [[self tasksList] objectAtIndex:[indexPath row]];

    static NSString *CellIdentifier = @"CellIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        // Do anything that should be the same on EACH cell here.  Fonts, colors, etc.
    }

    // Do anything that COULD be different on each cell here.  Text, images, etc.
    [[cell textLabel] setText:contentForThisRow];

    return cell;}

-(IBAction)startaddTask:(id)sender {

    addtaskview = [self.storyboard instantiateViewControllerWithIdentifier:@"addTaskView"];

    [self presentViewController:addtaskview animated:YES completion:nil];


}

- (IBAction)addTask:(id)sender {

    [tasksList addObject:self.taskName.text];

    [self dismissViewControllerAnimated:YES completion:nil];

    [mainTableView reloadData];
}

@end
6
  • Log and check the array count, you can know weather the object is added or not. Commented Dec 4, 2012 at 5:22
  • Which view you are dismissing dismissViewControllerAnimated ? And where is taskList and mainTableView is declared ? Commented Dec 4, 2012 at 5:22
  • in which class mainTableView ?? Commented Dec 4, 2012 at 5:22
  • Have you got tasksList = [[NSMutableArray alloc] init] anywhere in your code? Commented Dec 4, 2012 at 5:27
  • 1
    Share the code for tableview implementation, including the DataSource and Delegate. Without that, it wil be difficult to help. Commented Dec 4, 2012 at 5:35

3 Answers 3

1

Please make sure that you have done initialization of the taskList array. Like 'tasksList = [[NSMutableArray alloc] init]'.

Sign up to request clarification or add additional context in comments.

Comments

1

Did you try

[self dismissViewControllerAnimated:YES completion:^{
    [mainTableView reloadData];
}];

Also, put breakpoints and check if on call of reloadData, the TableView Delegate and DataSource methods are called.

Comments

0

I think, you are hardcoded numberOfRowsInSection please check, it will be -

              - (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
                     return [tasksList count];
                 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.