Hreno  Hrenovich
Hreno Hrenovich

Reputation: 336

How to access UITableViewCell object without creating *Cell.swift-file

I have tableView with dynamic cells.

In the tableViewCell I have 2 labels and 1 button

In function cellForRowAtIndexPath I'm adding action to it's button by such string of code:

btn?.addTarget(self, action: "downloadButtonClicked:", forControlEvents: .TouchUpInside)

And of course I have this function

func downloadButtonClicked(sender: AnyObject){

}

In this function I can access button of it's Cell by

(sender as UIButton)?.hidden = true //for example

But how can I access these labels from this function - downloadButtonClicked?

They have specific tag values but I do not know how to access them. A lot of variants with implementing "viewWithTag" did not satisfied me

I need the variant without creating specific file "MyTableViewCell.swift" for this cell

Upvotes: 0

Views: 1789

Answers (1)

iAnurag
iAnurag

Reputation: 9346

Find the button index first like this:

var btnPos: CGPoint = sender.convertPoint(CGPointZero, toView: self.tableView)
var indexPath: NSIndexPath = self.tableView.indexPathForRowAtPoint(btnPos)!

Then create cell using indexPath that we just got

 cell  = tableView.cellForRowAtIndexPath(indexPath) as! YourTableViewCellClass

Then you can access your label using

 cell.yourLabel.text = "anything"

Upvotes: 4

Related Questions