如何在tableview中创建动态单元格?

时间:2015-08-22 11:05:20

标签: ios objective-c uitableview

我在UITableView中遇到有关Dynamic cell的问题。我想在用户点击tableview中的按钮时创建动态行。

例如:在mytableview中有两行如下:

  1. 车:+
  2. 自行车:+
  3. 当用户点击添加按钮然后我必须在汽车单元下面再显示两行同样的东西,当用户在自行车前面点击添加按钮然后我必须再显示两个单元格。

2 个答案:

答案 0 :(得分:0)

您可以控制UITableViewDataSource Protocol中定义的UITableView's tableView:numberOfRowsInSection:函数中的数字行。只需定义一个整数变量,该变量保存用户应该看到的行数。在每个添加按钮上单击增加变量的值。

@implementation ViewController {
     int numberOfRows;
}

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

-(IBAction) addButtonClicked
{
    numberOfRows++;
}

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

答案 1 :(得分:0)

我认为你需要有2个部分,一个自行车和一个带有两个数据阵列的汽车部分来管理所有细胞的内容。您可能需要两种类型的单元格,一个“添加”单元格和一个“标准”单元格。

以下是一个例子:

- 在你的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];

要创建两种类型的单元格,请检查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;
}

不要忘记细胞/切片的数量:

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

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

然后你可以实现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];
}
相关问题