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;
}
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.
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.
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.
indexPath(for:)Returns an index path that represents the row and section of a specified table-view cell.
This line will handle repainting the cell, label and accessory for you:
[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With