Select First Row as default in UITableView

后端 未结 11 1735
悲哀的现实
悲哀的现实 2021-01-30 20:12

I have an application that is viewbased and I am adding a tableview as a subview to the main view. I have taken UITableViewDelegate to respond the table methods.

相关标签:
11条回答
  • 2021-01-30 20:27

    To only select the first cell the first time the table is loaded, one would think that using viewDidLoad is the right place to go, but, at that time of execution, the table hasn't loaded its contents, so it will not work (and probably crash the app since the NSIndexPath will point to a non-existent cell).

    A workaround is to use a variable that indicates that the table has loaded before and do the work accordingly.

    @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
    
    0 讨论(0)
  • 2021-01-30 20:33
    - (void)viewDidAppear:(BOOL)animated {
        [super viewDidAppear:animated];
        NSIndexPath *indexPath=[NSIndexPath indexPathForRow:0 inSection:0];
        [myTableView selectRowAtIndexPath:indexPath animated:YES  scrollPosition:UITableViewScrollPositionBottom];
    }
    

    The best way to use this in your code, if you want to select any row by default, use in viewDidAppear.

    0 讨论(0)
  • 2021-01-30 20:33

    Here's how to do this in Swift 1.2:

    override func viewWillAppear(animated: Bool) {
        let firstIndexPath = NSIndexPath(forRow: 0, inSection: 0)
        self.tableView.selectRowAtIndexPath(firstIndexPath, animated: true, scrollPosition: .Top)
    }
    
    0 讨论(0)
  • 2021-01-30 20:41

    Swift 4 Update:

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

    Change row and section values if you want to select any other row in a different section.

    0 讨论(0)
  • 2021-01-30 20:41

    Here is my solution for 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
    }
    
    0 讨论(0)
  • 2021-01-30 20:41

    You can do like this:

    - (void)viewDidLoad {
        [super viewDidLoad];
        NSIndexPath *ip=[NSIndexPath indexPathForRow:0 inSection:0];
        [myTableView selectRowAtIndexPath:ip animated:YES scrollPosition:UITableViewScrollPositionBottom];
    }
    
    0 讨论(0)
提交回复
热议问题