Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to highlight a row in a UITableView

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:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
   ...
   [cell.textLabel setHighlighted:YES];


   return cell;
}
like image 758
TheLearner Avatar asked Dec 23 '10 12:12

TheLearner


People also ask

What does indexPath row return?

So to start with the indexPath. row will be 0. Then it will be 1, then 2, then 3 and so on. You do this so that you can get the correct string from the array each time.

Is it possible to add UItableview within a Uitableviewcell?

Implementation of adding a table view inside the cell of UItableview aka, Nested Table View. Used the Power of Autosizing table view and delegate to achieve the expansion and collapse of the cell height.

What is indexPath UItableview?

IndexPath contains information about which row in which section the function is asking about. Base on this numbers you are configuring the cell to display the data for given row.

What is indexPath in Tableview Swift?

indexPath(for:)Returns an index path that represents the row and section of a specified table-view cell.


2 Answers

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

[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
like image 169
BoltClock Avatar answered Oct 19 '22 23:10

BoltClock


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

like image 32
JRG-Developer Avatar answered Oct 20 '22 00:10

JRG-Developer