2016-08-15 32 views
0

我有一個自定義的UITableViewCell在那裏我先下載它作爲填充的圖像上FB/Twitter上分享如下檢索從細胞的圖像使用斯威夫特

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() 

這裏的的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 
} 

圖像被正確上傳到適當的單元格中。我有一個共享按鈕,當按下時會打開一個操作表,它具有按鈕作爲FB等共享。我可以添加一個默認文本,當Facebook的東西彈出。 現在我想從按下分享按鈕的特定單元格中獲取圖像。我知道如何檢測按下哪個單元格的共享按鈕,但我無法從該單元格獲取圖像,因爲它沒有存儲在任何地方。

動作片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) 

} 

當feed.swift按下按鈕的報告功能被稱爲

這裏的feed.swift

protocol MyCustomCellDelegator { 
func report() 
} 

@IBAction func reportBut(sender: AnyObject) { 

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

} 

正如你可以看到我可以添加一個圖片whatever.png,並在facebook的東西彈出時顯示出來。我應該將所有單元格的圖像保存在某處,然後使用indexPath.row以某種方式訪問​​它?歡迎任何建議!

+0

您正在使用的UIImageView顯示圖像?您可以使用imageView.image從ImageView中獲取圖像。圖像必須存儲在某個地方,因爲它顯示在屏幕上。 – Yannick

+0

是的,我是(postingImage是一個UIImageView),但我不能在cellForRowAtIndexPath之外訪問它,因爲它是一個自定義單元格。 –

+0

您可以從Imageview中獲取圖像嗎? –

回答

0

向共享按鈕添加一個標籤,對應於cellForRowAtIndexPath中同一個單元格的indexPath.row。

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

當按鈕被按下時,你會得到一個refrence包含圖像細胞的indexPath.row所以讓我們獲得圖像。

//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 

讓我知道如果你需要更多的幫助

+0

嘿,我添加了更多的代碼。一探究竟!我想要訪問在cellForRowAtIndexPath –

+0

之外的貼圖像.image,我發佈的代碼將允許您訪問cellForRowAtIndexPath之外的postingImage.image。 –

+0

使用無法解析的標識符發件人... –