-1

In my application, there's a Navigation View(Embed In), a Table View Controller, and a View Controller. And in the ViewController.h, there are...

@property (strong, nonatomic) IBOutlet UILabel *timeDisplay;
@property (strong, nonatomic) IBOutlet UILabel *minuteDisplay;
@property (strong, nonatomic) IBOutlet UILabel *timerDisplay;
@property (strong, nonatomic) IBOutlet UILabel *titleLabel;

and more outlets. The code below is TableViewController.m file. I have a action button(plusbtn), which needs to add a cell in the table view. So, I tried to make the 'plusbtn' do it, but it didn't work. What should I do? Please help me...

#import "TableViewController.h"

#import "TableViewCell.h"

#import "ViewController.h"

@interface TableViewController ()

@property (nonatomic) ViewController *viewController;
@property (strong, nonatomic) NSMutableArray *titleArray;
@property (strong, nonatomic) NSMutableArray *tsArray;
@property (strong, nonatomic) NSMutableArray *tmArray;
@property (strong, nonatomic) NSMutableArray *ttArray;
@property (strong, nonatomic) NSString *cellplustitle;
@property (strong, nonatomic) NSString *cellplustime;
@property (strong, nonatomic) NSString *cellplusmin;
@property (strong, nonatomic) NSString *cellplussec;

@end

@implementation TableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    _cellplustitle = self.viewController.titleLabel.text;
    _cellplustime = self.viewController.timeDisplay.text;
    _cellplusmin = self.viewController.minuteDisplay.text;
    _cellplussec = self.viewController.timerDisplay.text;
    self.titleArray = [NSMutableArray new];
    self.tsArray = [NSMutableArray new];
    self.tmArray = [NSMutableArray new];
    self.ttArray = [NSMutableArray new];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return self.titleArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TableViewCell" forIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryNone;
    cell.celltitle.text = [self.titleArray objectAtIndex:indexPath.row];
    cell.cellmd.text = [self.tmArray objectAtIndex:indexPath.row];
    cell.cellsd.text = [self.tsArray objectAtIndex:indexPath.row];
    cell.celltd.text = [self.ttArray objectAtIndex:indexPath.row];

    // Configure the cell...

    return cell;
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)plusbtn:(id)sender {
    [self.tableView beginUpdates];

    [self.titleArray addObject:self.cellplustitle];
    [self.tsArray addObject:self.cellplussec];
    [self.tmArray addObject:self.cellplusmin];
    [self.ttArray addObject:self.cellplustime];
    NSIndexPath *indexPathOfNewItem = [NSIndexPath indexPathForRow:(self.titleArray.count - 1) inSection:0];
                         [self.tableView insertRowsAtIndexPaths:@[indexPathOfNewItem] withRowAnimation:UITableViewRowAnimationAutomatic];

                         [self.tableView endUpdates];
                         [self.tableView scrollToRowAtIndexPath:indexPathOfNewItem atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}
@end
홍승욱
  • 137
  • 2
  • 2
  • 8

2 Answers2

1

ReloadData should work. If its not working if you put if between beginUpdate and endupdate, then comment rest code and simply try to reload table view after adding item in array. Like...

- (IBAction)plusbtn:(id)sender {
[self.titleArray addObject:self.cellplustitle];
[self.tsArray addObject:self.cellplussec];
[self.tmArray addObject:self.cellplusmin];
[self.ttArray addObject:self.cellplustime];
[self.tableview reloadData];}

Hope this helps?

iOSGuest
  • 11
  • 1
0

you have to reload the uitableview using

[self.tableView reloadData];

Hope it helps!

ZAZ
  • 597
  • 3
  • 6