Select First Row as default in UITableView

后端 未结 11 1726
悲哀的现实
悲哀的现实 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:42
    - (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];
        }
    }
    
    0 讨论(0)
  • 2021-01-30 20:43
    - (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];
        }
    
    0 讨论(0)
  • 2021-01-30 20:47

    Swit 3.0 updated Solution

    let indexPath = IndexPath(row: 0, section: 0)
    tblView.selectRow(at: indexPath, animated: true, scrollPosition: .bottom)
    
    0 讨论(0)
  • 2021-01-30 20:48

    We use custom background images for the cell based on whether or not it is the first cell... a middle cell or the last cell. That way we get a nice rounded corner look to the whole table. When the row is selected, it swaps out a nice 'highlighted' cell to give the user feed back that they have selected a cell.

    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;
    

    If you wish just make the first cell, that which is at indexPath.row == 0, to use a custom background.

    This is derived from Matt Gallagher's excellent site

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

    Swift 5.x Update:

    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)
    }
    

    This is working for me and hope this will helps you.

    Happy Coding :)

    0 讨论(0)
提交回复
热议问题