Tequila_Tears Tequila_Tears - 3 months ago 11
Swift Question

Retrieving an image from a cell to share on FB/Twitter using Swift

I have a customised UITableViewCell where I'm populating an image by first downloading it as follows

let url = NSURL(string: imageurl1[indexPath.row])

let task = NSURLSession.sharedSession().dataTaskWithURL(url!) { (data, response, error) in

if error != nil
{
//
}
else
{
dispatch_async(dispatch_get_main_queue(), {

if let image = UIImage(data: data!) {

cell.postedImage.image = image
}

})

}


}
task.resume()


Here's the cellForRowAtIndexPath

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->
UITableViewCell{

var cell = UITableViewCell()

let cell = tableView.dequeueReusableCellWithIdentifier("feed1", forIndexPath: indexPath) as! feed

cell.delegate = self

if imageurl1[indexPath.row].isEmpty {

cell.postedImage.image = UIImage(named: "no_image.jpg")

}
else
{

let url = NSURL(string: imageurl1[indexPath.row])

let task = NSURLSession.sharedSession().dataTaskWithURL(url!) { (data, response, error) in

if error != nil
{
cell.postedImage.image = UIImage(named: "no_image.jpg")
}
else
{
dispatch_async(dispatch_get_main_queue(), {

if let image = UIImage(data: data!) {

cell.postedImage.image = image

}

})

}


}
task.resume()



}
cell.username.text = username1[indexPath.row]
**cell.reportPress.tag = indexPath.row**
}
return cell
}


The images get uploaded properly into appropriate cells. I have a share button which when pressed will open an action sheet and it has buttons as share on FB etc. I'm able to add a default text when the Facebook thing pops up. Now I want to get the image from that particular cell where the share button is pressed. I know how to detect which share button of which cell is pressed but I'm unable to get the image from that cell as it's not stored anywhere.

Action sheet for fb

func report() {

let actionSheetControllerIOS8: UIAlertController = UIAlertController()

let cancelActionButton: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel) { action -> Void in
print("Cancel")
}
actionSheetControllerIOS8.addAction(cancelActionButton)

let shareFBActionButton: UIAlertAction = UIAlertAction(title: "Share to Facebook", style: .Default)
{ action -> Void in
print("FB shared")

//////////////
if SLComposeViewController.isAvailableForServiceType(SLServiceTypeFacebook) {
var fbShare:SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)

fbShare.addImage(UIImage(named: "whatever.png"))

fbShare.setInitialText("Hello")
self.presentViewController(fbShare, animated: true, completion: nil)

} else {
var alert = UIAlertController(title: "Accounts", message: "Please login to a Facebook account to share.", preferredStyle: UIAlertControllerStyle.Alert)

alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
/////////////
}
actionSheetControllerIOS8.addAction(shareFBActionButton)

self.presentViewController(actionSheetControllerIOS8, animated: true, completion: nil)

}


The report function is called when a button is pressed in feed.swift

Here's feed.swift

protocol MyCustomCellDelegator {
func report()
}

@IBAction func reportBut(sender: AnyObject) {

self.delegate.report()
print(reportPress.tag)

}


As you can see I can add an image whatever.png and it shows up when the facebook thing pops up. Should I save the image for all the cells somewhere and then access it somehow using indexPath.row? Any suggestions are welcome!

Answer

add a tag to the share button that correspond to the indexPath.row of the same cell inside cellForRowAtIndexPath.

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
   fbShareButton.tag = indexPath.row
}

When the button is pressed you will get a refrence to the indexPath.row of the cell containing the image so lets get the image.

//sender is you button tht is pressed
let indexPath = NSIndexPath(forRow: sender.tag, inSection: 0)
let cell = tableView.cellForRowAtIndexPath(indexPath) as! feed

//you can now access the image like so
cell.postedImage.image

let me know if you need more help

Comments