Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get UITableViewCell object from indexPath in Swift?

I'm trying to get UITableViewCell object programmatically using Swift, so I wrote this code:

let cell:UITableViewCell = (UITableViewCell) UITableView.cellForRowAtIndexPath(indexPath.row) 

but getting compile error as:

Cannot convert value of type '(UITableViewCell).Type' to specified type 'UITableViewCell'
Consecutive statements on a line must be separated by ';'
Instance member 'cellForRowAtIndexPath' cannot be used on type 'UITableView'; did you mean to use a value of this type instead?

like image 665
shripad20 Avatar asked Aug 02 '14 11:08

shripad20


People also ask

How do I get IndexPath in Swift?

add an 'indexPath` property to the custom table cell. initialize it in cellForRowAtIndexPath. move the tap handler from the view controller to the cell implementation. use the delegation pattern to notify the view controller about the tap event, passing the index path.

How does Swift compare to IndexPath?

The Swift overlay to the Foundation framework provides the IndexPath structure, which bridges to the NSIndexPath class. This means that, as an alternative to NSIndexPath , starting with Swift 3 and Xcode 8, you can use IndexPath . Note that IndexPath also conforms to Equatable protocol. Therefore, you can use == or !=

What is IndexPath section in Swift?

Swift version: 5.6. Index paths describe an item's position inside a table view or collection view, storing both its section and its position inside that section.


1 Answers

cellForRowAtIndexPath is not a class method. Use the instance method instead.

let cell = tableView.cellForRowAtIndexPath(indexPath) 

Swift 3:

let cell = tableView.cellForRow(at: indexPath) 
like image 134
Mundi Avatar answered Oct 05 '22 01:10

Mundi