在集合视图中准备segue

时间:2015-07-24 08:44:14

标签: ios swift uicollectionview segue

我已经实现了集合视图但是有一个segue错误。我试图使用segue将图像传递给另一个视图控制器。这是它的代码。

public class MainActivity extends ActionBarActivity {

private Button button;
private String LOGIN_URL =
        "http://amjad-test.site40.net/arabictest.php";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button = (Button) findViewById(R.id.mybutton) ;

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            DownloadManager.Request request = new DownloadManager.Request(Uri.parse(LOGIN_URL));
            request.setTitle("File Downloading");
            request.setDescription("File is being Downloaded...");
            request.allowScanningByMediaScanner();
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
            String File_name = URLUtil.guessFileName(LOGIN_URL, null, MimeTypeMap.getFileExtensionFromUrl(LOGIN_URL));
            request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, File_name);
            DownloadManager manager = (DownloadManager) MainActivity.this.getSystemService(Context.DOWNLOAD_SERVICE);
            manager.enqueue(request);
            Toast.makeText(MainActivity.this, "Downloading", Toast.LENGTH_LONG).show();


        }
    });

   }

但是我在func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { self.performSegueWithIdentifier("showImage", sender: self) } override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if segue.identifier == "showImage" { let indexPaths = self.collectionView!.indexPathsForSelectedItems()! let indexPath = indexPaths[0] as NSIndexPath let vc = segue.destinationViewController as! NewViewController vc.image = self.imageArray[indexPath.row]! vc.title = self.appleProducts[indexPath.row] } } } 说错误,说无法找到成员indexPathsForSelectedItems。为什么会发生这种情况以及如何改变它。谢谢

1 个答案:

答案 0 :(得分:2)

我对您的代码进行了一些更改

在代码中添加以下更改

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath)
{
    self.performSegueWithIdentifier("showImage", sender: indexPath)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
    if segue.identifier == "showImage"
    {
        let indexPath : NSIndexPath = sender as! NSIndexPath           
        let vc = segue.destinationViewController as! NewViewController
        vc.image = self.imageArray[indexPath.row]!
        vc.title = self.appleProducts[indexPath.row]
    }
}