How to highlight a row in a UITableView

前端 未结 6 1103
名媛妹妹
名媛妹妹 2020-12-31 10:00

The code below seems to have no effect. I want it to be highlighed in the same way it highlights when you tap on a row

- (UITableViewCell *)tableView:(UITab         


        
相关标签:
6条回答
  • 2020-12-31 10:36

    You can use 

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    {
       ...
       [cell.textLabel setHighlighted:YES];
    
    
       return cell;
    }
    
    0 讨论(0)
  • 2020-12-31 10:42

    This line will handle repainting the cell, label and accessory for you:

    [tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
    
    0 讨论(0)
  • 2020-12-31 10:43

    You have two choices. Either use the selection of the table view:

    [tableView selectRowAtIndexPath: indexPath animated: YES scrollPosition: UITableViewScrollPositionNone];
    

    Or change the background color of the table cell in the table view delegate:

    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
        cell.backgroundColor = ...
    }
    
    0 讨论(0)
  • 2020-12-31 10:47

    As others have noted, you can programmatically select a cell using this method:

     [tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
    

    However, you should be careful not to call this method too soon.

    The first time that the view controller containing said tableView is initialized, the earliest that you should call this method is within viewDidAppear:.

    So, you should do something like this:

    - (void)viewDidAppear:(BOOL)animated
    {
       NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; // set to whatever you want to be selected first 
       [tableView selectRowAtIndexPath:indexPath animated:NO  scrollPosition:UITableViewScrollPositionNone];
    }
    

    If you try putting this call into viewDidLoad, viewWillAppear:, or any other early view lifecycle calls, it will likely not work or have odd results (such as scrolling to the correct position but not selecting the cell).

    0 讨论(0)
  • 2020-12-31 10:53
    - (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
       return YES;
    }
    
    - (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
      // do something here
    }
    

    Try this :)

    0 讨论(0)
  • 2020-12-31 10:59
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection: 0];
    
    [tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
    

    usually works

    If you are using interface builder to build your interface, one thing to check is that you specified the view of the table view controller. If you don't actually specify the view, this selection message may go nowhere.

    (you specify the view by selecting the table view controller in the interface builder inspector window and dragging the outlet for "view" to the table view you want to do the selection in).

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