Hi there I have situation where in an iPad app my master controller
has list and details controller have its details , a typical UISplitViewController
pattern.
What I want to achieve is , my first row should be initially selected and later I want to give selection choice to user.
I am using cell's setSelected
and in didSelectRowAtIndexPath
method removing selection like this.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
NSIndexPath* path = [NSIndexPath indexPathForRow:0 inSection:0];
UITableViewCell* cell = [tableView cellForRowAtIndexPath:path];
[cell setSelected:NO];
}
for initial cell but my none cell is getting selected after that please help me out.
For the cell to appear selected, you have to call -setSelected:animated:
from within -tableView:willDisplayCell:forRowAtIndexPath:
like so:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (/* should be selected */) {
[cell setSelected:YES animated:NO];
}
}
Calling -setSelected
from anywhere else has no effect.
try this
NSIndexPath* selectedCellIndexPath= [NSIndexPath indexPathForRow:0 inSection:0];
[self tableView:tableViewList didSelectRowAtIndexPath:selectedCellIndexPath];
[tableViewList selectRowAtIndexPath:selectedCellIndexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
OR
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (/* should be selected */) {
[cell setSelected:YES animated:NO];
}
}
Swift 4 version is
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
func shouldSelect() -> Bool {
// Some checks
}
if shouldSelect() {
tableView.selectRow(at: indexPath, animated: false, scrollPosition: .none)
}
}
I don't know you are expecting this answer. By default if you wants to select first row in your tableView with out manual selection, just initiate the didSelectRowAtIndexPath
method like this
- (void)viewDidAppear:(BOOL)animated {
NSIndexPath *indexPath=[NSIndexPath indexPathForRow:0 inSection:0];
[myTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionBottom];
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if shouldSelectThisRow {
tableView.selectRow(at: indexPath, animated: false, scrollPosition: .none)
} else {
tableView.deselectRow(at: indexPath, animated: false)
}
}
Swift 4: Selecting cell initially
import UIKit
class MyViewController: UIViewController, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
...
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if /* your code here */ == indexPath.row {
tableView.selectRow(at: indexPath, animated: false, scrollPosition: UITableView.ScrollPosition.none)
}
}
...
}
It will also help in the case of POP Navigation
.h file,
NSIndexPath *defaultSelectedCell
.m File
Put this code in ViewDidLoad
defaultSelectedCell= [NSIndexPath indexPathForRow:0 inSection:0];
in viewDidAppear
[self.tableView selectRowAtIndexPath:defaultSelectedCell animated:NO scrollPosition:UITableViewScrollPositionNone];
This will help when you POP, Put this code in viewWillAppear
[self.catTableView selectRowAtIndexPath:defaultSelectedCell animated:NO scrollPosition:UITableViewScrollPositionNone];
in tableView didSelectRowAtIndexPath
Method,
defaultSelectedCell = [NSIndexPath indexPathForRow:indexPath.row inSection:0];
This helps me perfectly either first time loading or Pop Navigation.
Swift 4 and 5: Selecting cells initially just one time without willDisplayCell
resetting their status:
override func viewWillAppear(_ animated: Bool) {
for section in 0..<tableView.numberOfSections {
for row in 0..<tableView.numberOfRows(inSection: section) {
let indexPath = IndexPath(row: row, section: section)
if /* your condition for selecting here */ {
_ = tableView.delegate?.tableView?(tableView, willSelectRowAt: indexPath) // remove if you don't want to inform the delegate
tableView.selectRow(at: indexPath, animated: false, scrollPosition: .none)
tableView.delegate?.tableView?(tableView, didSelectRowAt: indexPath) // remove if you don't want to inform the delegate
}
}
}
}
Swift 5, Thanks @Ravindhiran's answer a lot:
let selectedCellIndexPath = IndexPath(row: 0, section: 0)
tableView(tableViewList, didSelectRowAt: selectedCellIndexPath)
tableViewList.selectRow(at: selectedCellIndexPath, animated: false, scrollPosition: .none)
or
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if /* should be selected */{
cell.setSelected(true, animated: false)
}
}
Swift 5.2
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: true)
accessoryType = selected ? .checkmark : .none
backgroundColor = selected ? UIColor.secondarySystemBackground : UIColor.systemBackground
myLabel.textColor = selected ? .tertiaryLabel : .secondaryLabel
self.isUserInteractionEnabled = selected ? false : true
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
let cell = cell as! CustomTableViewCell
if (/* condition is met */) {
cell.setSelected(true, animated: true)
}
}
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