简体   繁体   English

如何在表视图中创建动态单元格?

[英]How to create dynamic cell in tableview?

I am facing issue regarding Dynamic cell in UITableView. 我面临有关UITableView中动态单元格的问题。 I want to create dynamic rows while user clicked on button in tableview. 我想在用户单击表格视图中的按钮时创建动态行。

Eg: in mytableview there are two rows as following : 例如:在mytableview中有两行,如下所示:

  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. 您可以在UITableView的tableView:numberOfRowsInSection:函数中控制数字行,该函数在UITableViewDataSource协议中定义。 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: 要创建两种类型的单元格,请检查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: 然后可以实现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];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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