2015-08-22 55 views
0

我正面臨有關UITableView中動態單元的問題。我想在用戶點擊tableview中的按鈕時創建動態行。如何在tableview中創建動態單元格?

例如:在mytableview有兩行如下:

  1. 汽車:+
  2. 自行車:+

當用戶點擊添加按鈕,然後我必須表現出以下兩個行汽車細胞同樣的事情,而用戶點擊自行車前添加按鈕,然後我必須顯示兩個更多的細胞。

回答

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]; 
} 
+0

我無法做到這一點,你可以共享整個代碼。 – MRJ

+0

我添加了缺失的單元格/部分數量,並且您擁有所有內容:) –

+0

非常感謝您分享您的代碼。我需要你的幫助,我想只顯示自行車的數據:Bajaj,本田,鈴木模型,而用戶點擊添加按鈕並保存。 – MRJ

0

您可以控制在UITableViewDataSource協議中定義的​​函數中的數字行。只需定義一個整數變量來保存用戶應該看到的數字行。在每個添加按鈕上單擊增加變量的值。

@implementation ViewController { 
    int numberOfRows; 
} 

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

-(IBAction) addButtonClicked 
{ 
    numberOfRows++; 
} 

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

我必須表現出三排後點擊添加按鈕,現在我這樣做的方法是創建行,但我以前的行刪除了與顯示新行,但我需要一個像擴大視圖。它應該顯示我以前的常數,當點擊添加按鈕時,它應該逐一增加我的行數,Bike行將不斷顯示我什麼時候會做我以前的活動。 – MRJ

+0

在這種情況下,您可以將numberOfRows_值連接到一個函數,該函數計算應該在那裏存在多少行。 - (int)numberOfRows { int numberOfBikeRows = 3; int numberAddedWithButton = 2; ..做你自己的實現 } –

相關問題