【问题标题】:Creating a static "add" cell at index 0 of dynamic UITableView cells在动态 UITableView 单元格的索引 0 处创建静态“添加”单元格
【发布时间】:2015-04-10 15:38:51
【问题描述】:

我有一个应用程序可以将兴趣点 (POI) 添加到 managedObjectContext。在AddPOITableViewController 上,我有一个转到CategoryTableViewController 的segue,CategoryTVC 显示managedObjectContext 中的现有类别。

导航如下:

POIList-->AddPOIViewController-->CategoryTableViewController-->AddCategoryVC

问题: 因为我在managedObjectContext 中没有任何类别,所以我的CategoryTVC 上没有显示任何类别。

我想做的事:CategoryTVC 上创建一个特殊的单元格,它与AddCategoryTVC 相连,我可以在managedObjectContext 中创建一个新类别。

我知道我需要在index[0] 添加一个特殊的单元格,与我单击时连续。我想把它从我的category.count 中剔除。我会用numberOfRowsInSection(例如:category.count+1)为我的特殊单元格添加额外的空间。对于 AddCategoryVC 的 segue,我必须以不同的方式处理我的“添加类别”单元格(我会设置为 index[0]

任何想法我将如何做到这一点将不胜感激。

【问题讨论】:

    标签: ios objective-c uitableview core-data


    【解决方案1】:

    didSelectRowAtIndexPath 回调中的 indexPath 对象告诉您单击了哪个单元格。只需检查此对象以查看它是否是您的“添加类别”单元格,如果是,请执行另一个 segue。像这样:

    - (void)tableView:(UITableView *)tableview didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if(indexPath.row == <myAddRowIndex>)
        {
             [self performSegueWithIdentifier:@"AddCategorySegue" sender:self];
        }
        else
        {
             [self performSegueWithIdentifier:@"pointOfInterestSegue" sender:self];
        }
    }
    

    【讨论】:

      【解决方案2】:

      我发现这篇文章有助于我完成任务:

      Combine static and prototype content in a table view

      在这种情况下,我实际上是在 indexPath.row[0] 处创建一个静态单元格,其余内容是动态的并从 managedObjectContext 中提取。

      这是我最终做的:

      // Added this constant
      #define NUMBER_OF_STATIC_CELLS 1
      
      // Added these properties
      static NSString *DynamicIdentifier = @"DynamicIdentifier";
      static NSString *StaticIdentifier = @"StaticIdentifier";
      
      // Tweaked numberOfRowsInSection to add an extra cell
      - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
          // Return the number of rows in the section.
          return self.locationCategories.count + NUMBER_OF_STATIC_CELLS;
      }
      
      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
      
          if (indexPath.row < NUMBER_OF_STATIC_CELLS) {
              UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:StaticIdentifier];
      
              // this sets up the static cell at the top
              if (cell == nil) {
                  cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:StaticIdentifier];
              }
      
              cell.textLabel.text = @"Add Category";
              cell.textLabel.textColor = [UIColor redColor];
              return cell;
      
          } else {
      
              // this sets up the dynamic cells below
              UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:DynamicIdentifier forIndexPath:indexPath];
              // Configure the cell...
              NSManagedObject *locationCategory = [self.locationCategories objectAtIndex:indexPath.row];
              cell.textLabel.text = [locationCategory valueForKey:@"categoryName"];
      
              if (locationCategory == self.category.categoryName) {
                  cell.accessoryType = UITableViewCellAccessoryCheckmark;
              } else {
                  cell.accessoryType = UITableViewCellAccessoryNone;
              }
              return cell;
          }
      }
      

      这是我的AddCategoryViewController 发生转场的地方。

      - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
      
          // If there was a previous selection, unset the accessory view for its cell.
          NSManagedObject *currentCategory = self.category.categoryName;
      
          if (currentCategory != nil && indexPath.row > NUMBER_OF_STATIC_CELLS - 1) {
              NSInteger index = [self.locationCategories indexOfObject:currentCategory];
              NSIndexPath *selectionIndexPath = [NSIndexPath indexPathForRow:index inSection:0];
              UITableViewCell *checkedCell = [tableView cellForRowAtIndexPath:selectionIndexPath];
              checkedCell.accessoryType = UITableViewCellAccessoryNone;
          }
      
          if (indexPath.row > NUMBER_OF_STATIC_CELLS - 1) {
              // Set the checkmark accessory for the selected row.
              [[tableView cellForRowAtIndexPath:indexPath]setAccessoryType:UITableViewCellAccessoryCheckmark];
      
              // Update the type of the location category instance
              self.category.categoryName = [self.locationCategories objectAtIndex:indexPath.row];
      
              // Deselect row
              [tableView deselectRowAtIndexPath:indexPath animated:YES];
          } else {
              [self performSegueWithIdentifier:@"addCategory" sender:nil];
              NSLog(@"addCategory segue fired from didSelectRowAtIndexPath method");
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多