UITableViewCell Set selected initially
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)
}
}
Set default selected cell in UITableViewController
try this:
class TableViewController: UITableViewController {
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("CustomCell", forIndexPath: indexPath)
// setup selectedBackgroundView
let backgroundSelectionView = UIView()
backgroundSelectionView.backgroundColor = UIColor.whiteColor().colorWithAlphaComponent(0.1)
cell.selectedBackgroundView = backgroundSelectionView
return cell
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
let indexPath = NSIndexPath(forRow: 0, inSection: 0)
tableView.selectRowAtIndexPath(indexPath, animated: false, scrollPosition: .None)
}
}
How to set selected some index of TableView
First select multipleSelection checkmark in storyboard, or by code self.testTableView.allowsMultipleSelection = true
, then after tableview.reloadData put this 2 lines
self.testTableView.selectRow(at: IndexPath(row: 0, section: 0), animated: false, scrollPosition: .none)
self.testTableView.selectRow(at: IndexPath(row: 2, section: 0), animated: false, scrollPosition: .none)
Standard Code
self.testTableView.reloadData()
self.testTableView.allowsMultipleSelection = true
//here is where selection is made
self.testTableView.selectRow(at: IndexPath(row: 0, section: 0), animated: false, scrollPosition: .none)
self.testTableView.selectRow(at: IndexPath(row: 2, section: 0), animated: false, scrollPosition: .none)
Specified this Example Code
var variable = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
import UIKit
class ControllerClass: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var testTableView: UITableView!
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return variable.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "testCell", for: indexPath) as! TestTableViewCell
cell.accessoryType = .none
cell.lblWordText?.text = variable[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
}
override func viewWillAppear(_ animated: Bool) {
self.tabBarController?.tabBar.isHidden = true
}
override func viewDidAppear(_ animated: Bool){
super.viewDidAppear(animated)
self.testTableView.allowsMultipleSelection = true
self.testTableView.reloadData()
//here is where selection is made
self.testTableView.selectRow(at: IndexPath(row: 0, section: 0), animated: false, scrollPosition: .none)
self.testTableView.selectRow(at: IndexPath(row: 2, section: 0), animated: false, scrollPosition: .none)
}
override func viewDidLoad() {
testTableView.dataSource = self
}
}
Hope this helps
Set Selected/ Unselected view color in UITableView in Swift
You need to override
setSelected(_:animated:)
method in TableViewCell
and configure the backgroundColor
based on selected
state.
class TableViewCell: UITableViewCell {
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
self.backgroundColor = selected ? .red : .clear
}
}
No need to change the backgroundColor
in tableView(_:didSelectRowAt:) method
.
Selecting first row in UITableViewController in Swift
You forgot implement func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
method.
And you needn't to use delegate
when call didSelectRowAtIndexPath
:
if(indexPath.row == 0)
{
let rowToSelect:NSIndexPath = NSIndexPath(forRow: 0, inSection: 0)
tableView.selectRowAtIndexPath(rowToSelect, animated: true, scrollPosition: UITableViewScrollPosition.None)
self.tableView(tableView, didSelectRowAtIndexPath: rowToSelect)
}
=== EDITED ===
Change your didDeselectRowAtIndexPath
to didSelectRowAtIndexPath
.
How to select UITableView cell programmatically (Swift 2)
Try to use viewDidAppear
func viewDidAppear(animated: Bool){
super.viewDidAppear(animated)
let path = NSIndexPath(forRow: 1, inSection: 0)
tableView.selectRowAtIndexPath(myPath, animated: false, scrollPosition: UITableViewScrollPosition.None)
}
Related Topics
Custom Class Clusters in Swift
How to Determine the True Data Type of an Nsnumber
Eraser Not Working in iOS Drawing
Swiftui Landmarks App Tutorial Screen Navigates Back When Toggle Favorite
Application Not Automatically Launching in Device and Getting Error on Xcode
Add a Watermark on Video After Merging Video and Audio Asset into One in Swift3 iOS
iPhone Different Screen Sizes in Flash? (Getting Black Bars)
Presenting Uiviewcontroller from Skscene
What Is Most Common and Correct Practice to Get a Cgfloat from an Nsnumber
Swift Override Function in Extension
Uiview's Border Color in Interface Builder Doesn't Work
Rotate Uiview Around Its Center Keeping Its Size
How to Get the Frame of a View Inside Another View
Change Frame Programmatically with Auto Layout
Uitableviewrowaction Image for Title
How to Get HTML Source from Url with Swift
Xcode 8.1 Push Notifications in Swift 2.3 with Firebase Integration Not Getting