简体   繁体   中英

How to create dynamic cell in tableview?

I am facing issue regarding Dynamic cell in UITableView. I want to create dynamic rows while user clicked on button in tableview.

Eg: in mytableview there are two rows as following :

  1. Car :+
  2. Bike :+

When user clicked add button then I have to show two more rows below car cell same thing as while user clicked on in front of bike add button then I have to show two more cells.

You can control number rows in UITableView's tableView:numberOfRowsInSection: function that is defined in UITableViewDataSource Protocol. Just define an integer variable that holds the number rows user should see. On every Add button click increase the value of variable.

@implementation ViewController {
     int numberOfRows;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    numberOfRows = 3;
}

-(IBAction) addButtonClicked
{
    numberOfRows++;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return numberOfRows;
}

I think you need to have 2 sections, a bike and a car section with two data arrays to manage content of all cells. You may need to have two types of cell, an "add" cell and a "standard" cell.

Here is an example:

-In your viewDidiLoad:

self.bikeArray = [NSMutableArray arrayWithArray:@[@"Add bike"]]; // Aray to fill section 0
self.carArray = [NSMutableArray arrayWithArray:@[@"Add car"]]; // Aray to fill section 1
self.typeOfVehicleArray = @[@"Bike", @"Car"];
self.dataArray = @[self.bikeArray, self.carArray];

To create two types of cell, check indexPath.row:

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

    UITableViewCell *cell;
    switch (indexPath.row) {
        case 0:{
            NSString *addIdentifier = @"addIdentifier";
            cell = [tableView dequeueReusableCellWithIdentifier:addIdentifier];
            if (!cell){
                // Add button...
                CGFloat buttonWidth = 60.f;
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:addIdentifier];
                UIButton *b = [[UIButton alloc] initWithFrame:CGRectMake(cell.frame.size.width - buttonWidth - 10.f , 5.f, buttonWidth, cell.frame.size.height - 10.f)];
                [b setTitle:@"ADD" forState:UIControlStateNormal];
                // ...with a wonderful color
                b.backgroundColor = [UIColor greenColor];
                [b addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
                [cell addSubview:b];
                [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
            }

            break;
        }
        default:{
           NSString *standardIdentifier = @"standardIdentifier";
           cell = [tableView dequeueReusableCellWithIdentifier:standardIdentifier];
            if (!cell){
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:standardIdentifier];
                [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
            }
            break;
        }
    }

    NSArray *vehicleArray = self.dataArray[indexPath.section];
    cell.textLabel.text = [vehicleArray objectAtIndex:indexPath.row];

    return cell;
}

Don't forget number of cells / sections:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [self.dataArray count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *vehicleArray = self.dataArray[section];
    return [vehicleArray count];
}

Then you can implement buttonPressed:

- (void) buttonPressed: (id)sender{
    // Find the section
    UIButton *b = (UIButton *)sender;
    UITableViewCell *cell = (UITableViewCell *)[b superview];
    NSInteger section = [self.tableView indexPathForCell:cell].section;

    // Get vehicle array (bikeArray or carArray)
    NSMutableArray *vehicleArray = self.dataArray[section];
    NSString *vehicleType = [self.typeOfVehicleArray objectAtIndex:section];
    NSInteger nextVehicle = [vehicleArray count];
    NSString *firstRowToAdd = [NSString stringWithFormat:@"%@ %lu", vehicleType, (long)nextVehicle];
    NSString *secondRowToAdd = [NSString stringWithFormat:@"%@ %lu", vehicleType, (long)(nextVehicle + 1)];

    // Add object in vehicle array
    [vehicleArray addObject:firstRowToAdd];
    [vehicleArray addObject:secondRowToAdd];

    // Create array of corresponding indexPath and update tableview
    NSArray *indexPathArray = @[[NSIndexPath indexPathForRow:nextVehicle inSection:section],
                            [NSIndexPath indexPathForRow:(nextVehicle + 1) inSection:section]];
    [self.tableView beginUpdates];
    [self.tableView insertRowsAtIndexPaths:indexPathArray withRowAnimation:UITableViewRowAnimationRight];
    [self.tableView endUpdates];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM