【问题标题】:Select First Row as default in UITableView在 UITableView 中选择第一行作为默认值
【发布时间】:2011-02-13 06:19:43
【问题描述】:

我有一个基于视图的应用程序,我正在将一个表视图作为子视图添加到主视图。我已经采取UITableViewDelegate 来响应表格方法。一切正常,但我想选择第一行或UITableView 作为默认选择(突出显示)。

请帮助我,我需要什么代码以及我需要把它放在哪里。

【问题讨论】:

  • 我也在试图弄清楚如何做到这一点。它需要在调用 UITableViewDataSource 函数后设置,以便表格视图中有一些内容。 UITableView 似乎没有任何在数据加载后触发的回调。
  • 您可以按照正确答案解释here 进行操作,您还需要将选定的索引存储在某处,以便在滚动表格视图时,它的行为如您所愿

标签: ios uitableview uikit


【解决方案1】:
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    NSIndexPath *indexPath=[NSIndexPath indexPathForRow:0 inSection:0];
    [myTableView selectRowAtIndexPath:indexPath animated:YES  scrollPosition:UITableViewScrollPositionBottom];
}

在您的代码中使用它的最佳方式,如果您想默认选择任何行,请在 viewDidAppear 中使用。

【讨论】:

  • 由于某种奇怪的原因,我的单元格不会在 viewDidAppear 中选择,但在 viewWillAppear 中可以正常工作
  • 如果有一些过滤器正在发生,应用程序可能会崩溃并导致空数组。需要在开始时添加检查
  • 我正在使用这种方法首先选择一个单元格,然后我正在调用委托,但使用 selectRowAtIndexPath 选择的单元格永远不会被取消选择。不允许多重选择。有什么帮助吗?请,谢谢。
  • 单元格永远不会被取消选择,甚至不会调用 deselectRowAtIndexPath
  • 已解决。在 viewDidAppear 上调用 selectRowAtIndexPath 而不是 viewDidLoad。对不起,谢谢
【解决方案2】:

Swit 3.0 更新解决方案

let indexPath = IndexPath(row: 0, section: 0)
tblView.selectRow(at: indexPath, animated: true, scrollPosition: .bottom)

【讨论】:

    【解决方案3】:
    - (void)viewWillAppear:(BOOL)animated
        {
    
           [super viewWillAppear:animated];
    
         // assuming you had the table view wired to IBOutlet myTableView
    
            // and that you wanted to select the first item in the first section
    
            [myTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:0];
        }
    

    【讨论】:

      【解决方案4】:
      - (void)viewDidLoad
      {
          [super viewDidLoad];
          self.detailViewController = (DetailViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];
      
          if([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad){
              NSIndexPath* indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
              [self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionTop];
              [self tableView:self.tableView didSelectRowAtIndexPath:indexPath];
          }
      }
      

      【讨论】:

        【解决方案5】:

        Swift 4 更新:

        func viewDidAppear(_ animated: Bool) {
            super.viewDidAppear(animated)
            let indexPath = IndexPath(row: 0, section: 0)
            myTableView.selectRow(at: indexPath, animated: true, scrollPosition: .bottom)
        }
        

        如果您想选择不同部分中的任何其他行,请更改行和部分值。

        【讨论】:

          【解决方案6】:

          以下是在 Swift 1.2 中执行此操作的方法:

          override func viewWillAppear(animated: Bool) {
              let firstIndexPath = NSIndexPath(forRow: 0, inSection: 0)
              self.tableView.selectRowAtIndexPath(firstIndexPath, animated: true, scrollPosition: .Top)
          }
          

          【讨论】:

            【解决方案7】:

            这是我对 swift 3.0 的解决方案:

            var selectedDefaultIndexPath = false
            
            
            override func viewDidAppear(_ animated: Bool) {
                super.viewDidAppear(animated)
            
                if dataSource.isEmpty == false, selectedDefaultIndexPath == false {
                    let indexPath = IndexPath(row: 0, section: 0)
                    // if have not this, cell.backgroundView will nil.
                    tableView.selectRow(at: indexPath, animated: false, scrollPosition: .none)
                    // trigger delegate to do something.
                    _ = tableView.delegate?.tableView?(tableView, willSelectRowAt: indexPath)
                    selectedDefaultIndexPath = true
                }
            }
            
            func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
                let cell = tableView.cellForRow(at: indexPath)
                cell?.selectedBackgroundView?.backgroundColor = UIColor(hexString: "#F0F0F0")
            
                return indexPath
            }
            

            【讨论】:

            • 委托调用绝对解决了我没有创建 selectedBackgroundView 的问题。我不得不修改 willSelect 来重新创建我的视图。覆盖 func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? { let cell = tableView.cellForRow(at: indexPath) let bgColorView = UIView() bgColorView.backgroundColor = #colorLiteral(red: 1, green: 0.596, blue: 0.004, alpha: 1) cell?.selectedBackgroundView = bgColorView return indexPath }
            【解决方案8】:

            Swift 5.x 更新:

            override func viewDidAppear(_ animated: Bool) {
                super.viewDidAppear(animated)
                let indexPath = IndexPath(row: 0, section: 0)
                tableView.selectRow(at: indexPath, animated: true, scrollPosition: .bottom)
                tableView.delegate?.tableView?(tableView, didSelectRowAt: indexPath)
            }
            

            【讨论】:

              【解决方案9】:

              我们根据单元格是第一个单元格...中间单元格还是最后一个单元格来为单元格使用自定义背景图像。这样我们就可以看到整个桌子的圆角。选择该行后,它会换出一个漂亮的“突出显示”单元格,以向用户反馈他们选择了一个单元格。

              UIImage *rowBackground;
              UIImage *selectionBackground;
              NSInteger sectionRows = [tableView numberOfRowsInSection:[indexPath section]];
              NSInteger row = [indexPath row];
              
              if (row == 0 && row == sectionRows - 1)
              {
                  rowBackground = [UIImage imageNamed:@"topAndBottomRow.png"];
                  selectionBackground = [UIImage imageNamed:@"topAndBottomRowSelected.png"];
              }
              else if (row == 0)
              {
                  rowBackground = [UIImage imageNamed:@"topRow.png"];
                  selectionBackground = [UIImage imageNamed:@"topRowSelected.png"];
              }
              else if (row == sectionRows - 1)
              {
                  rowBackground = [UIImage imageNamed:@"bottomRow.png"];
                  selectionBackground = [UIImage imageNamed:@"bottomRowSelected.png"];
              }
              else
              {
                  rowBackground = [UIImage imageNamed:@"middleRow.png"];
                  selectionBackground = [UIImage imageNamed:@"middleRowSelected.png"];
              }
              
              
              ((UIImageView *)cell.backgroundView).image = rowBackground;
              ((UIImageView *)cell.selectedBackgroundView).image = selectionBackground;
              

              如果您只想让 indexPath.row == 0 处的第一个单元格使用自定义背景。

              这源自马特·加拉格尔的excellent site

              【讨论】:

                【解决方案10】:

                第一次加载表格时只选择第一个单元格,有人会认为使用viewDidLoad是正确的去处,但是,在执行时,表格没有'没有加载它的内容,所以它不会工作(并且可能会使应用程序崩溃,因为NSIndexPath 将指向一个不存在的单元格)。

                一种解决方法是使用一个变量来指示该表之前已加载并相应地执行工作。

                @implementation MyClass {
                    BOOL _tableHasBeenShownAtLeastOnce;
                }
                
                - (void)viewDidLoad
                {
                    [super viewDidLoad];
                    _tableHasBeenShownAtLeastOnce = NO; // Only on first run
                }
                
                - (void)viewDidAppear:(BOOL)animated
                {
                    [super viewDidAppear:animated];
                
                    if ( ! _tableHasBeenShownAtLeastOnce )
                    {
                        _tableHasBeenShownAtLeastOnce = YES;
                        BOOL animationEnabledForInitialFirstRowSelect = YES; // Whether to animate the selection of the first row or not... in viewDidAppear:, it should be YES (to "smooth" it). If you use this same technique in viewWillAppear: then "YES" has no point, since the view hasn't appeared yet.
                        NSIndexPath *indexPathForFirstRow = [NSIndexPath indexPathForRow:0 inSection: 0];
                
                        [self.tableView selectRowAtIndexPath:indexPathForFirstRow animated:animationEnabledForInitialFirstRowSelect scrollPosition:UITableViewScrollPositionTop];
                    }
                }
                
                /* More Objective-C... */
                
                @end
                

                【讨论】:

                  猜你喜欢
                  • 2022-01-14
                  • 1970-01-01
                  • 1970-01-01
                  • 2011-03-31
                  • 2013-05-12
                  • 1970-01-01
                  • 2017-10-31
                  • 2020-10-18
                  • 1970-01-01
                  相关资源
                  最近更新 更多