检索从细胞的图像使用斯威夫特
我有一个自定义的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以某种方式访问它?欢迎任何建议!
向共享按钮添加一个标签,对应于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
让我知道如果你需要更多的帮助
嘿,我添加了更多的代码。一探究竟!我想要访问在cellForRowAtIndexPath –
之外的贴图像.image,我发布的代码将允许您访问cellForRowAtIndexPath之外的postingImage.image。 –
使用无法解析的标识符发件人... –
您正在使用的UIImageView显示图像?您可以使用imageView.image从ImageView中获取图像。图像必须存储在某个地方,因为它显示在屏幕上。 – Yannick
是的,我是(postingImage是一个UIImageView),但我不能在cellForRowAtIndexPath之外访问它,因为它是一个自定义单元格。 –
您可以从Imageview中获取图像吗? –