Dynamically add tableview cell one by one by button click in a single tableview

2.7k views Asked by At

i. I have tried a lot but could not execute successfully . ii. In tableview cell, there are 3 3 fields need to display. One image view, button1 -->taking photo button, button2---> browse button. iii. First time tableview should display a custom cell with one row. iv. When a user clicks "add new button" , which lays outside of tableview, a new row will create with all above 3 fields (image view, button1, button2) v. Number of clicks of "add new button" , will create new rows with all above 3 fields. vi. I could do all above things created dynamically successfully with a simple image view which contains above 3 fields but could not succeed to work on custom cell.

vii. Again I need to set the tag of each cell, broswe button, take photo button so that when clicks, will take the tag value.

1

There are 1 answers

0
Matic Oblak On

The table view works by adding a delegate and a data source. Assuming your table view has an owner as a view controller and both delegate and data source are the view controller itself. All you need to do is implement those data source methods to return an appropriate data then you should call reloadData on the table view or if you want a bit of extra work to look nicer check how to add rows animated around the web.

This is a very simple and not optimized example but is very short and easy to read. I hope it helps you get on the right track:

@interface MyViewController()<UITableViewDataSource, UITableViewDelegate>

@property UITableView *tableView;
@property NSArray *myCells;

@end

@implementation MyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.delegate = self; // could be done in storyboard
    self.tableView.dataSource = self; // could be done in storyboard
    [self addACell];
}
- (void)addCellButtonPressed:(id)sender {
    [self addACell];
}
- (void)addACell {
    MyCell *cell = [[MyCell alloc] init];
    [cell.button1 addTarget:self action:@selector(cellButton1Pressed:) forControlEvents:UIControlEventTouchUpInside];
    [cell.button2 addTarget:self action:@selector(cellButton2Pressed:) forControlEvents:UIControlEventTouchUpInside];
    self.myCells = [self.myCells arrayByAddingObject:cell];
    [self.tableView reloadData]; // will call the delegate again and refresh cells
}
- (void)cellButton1Pressed:(id)sender {
    MyCell *cellPressed = nil;
    for(MyCell *cell in self.myCells) {
        if(cell.button1 == sender) {
            cellPressed = cell;
            break;
        }
    }
    // do whatever
}
- (void)cellButton2Pressed:(id)sender {
    MyCell *cellPressed = nil;
    for(MyCell *cell in self.myCells) {
        if(cell.button2 == sender) {
            cellPressed = cell;
            break;
        }
    }
    // do whatever
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.myCells.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.myCells[indexPath.row];
}

@end