0

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!

  • Are you using UIImageView to show the image? You can get the image from the ImageView with imageView.image. The image must be stored somewhere because it is shown on the screen. – Yannick Aug 15 '16 at 09:37
  • Yes I am (postedImage is a UIImageView) but I can't access it outside the cellForRowAtIndexPath as it's a customised cell. – Tequila_Tears Aug 15 '16 at 09:42
  • You can fetch the image from Imageview right? – Mohammed Shakeer Aug 15 '16 at 09:44
  • If there's no cell involved, yes I can. But since the postedImage is a UIImageView in a customised cell file called feed.swift, I can access it using cell.postedImage in cellForRowAtIndexPath but not outside it – Tequila_Tears Aug 15 '16 at 09:57
  • First get the cell using cellForRowAtIndexPath: using the indexPath, then from the cell you can get the image. – Mohammed Shakeer Aug 15 '16 at 09:59

1 Answers1

0

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

Med Abida
  • 1,214
  • 11
  • 31