将Int(number)从tableViewCell传递给新的ViewController

时间:2015-11-04 00:07:18

标签: ios swift uitableview uiscrollview

这有点傻,但我有一个自定义tableViewCell,它有一个UIScrollView和pageControl。一切正常,但我无法弄清楚如何将在UITableViewCell中更新的“pageControll.currentPage”发送/传递给我的ViewImageViewController。

我没有使用“didSelectRowAtIndexPath”。每个单元格都有独立的变化图像scrollView和pageControl。

TableViewCell:完美运行

this.loginClickedStream
    .doOnNext(function() {
        if (!connectedToWifi) {
            //Skip to subscribe passing some data somehow
            skipToSubscribe({error: 'NotConnectedToWifi'});
        } 
    })
    .flatMap(function() {
        return fetch(url);
    })
    .subscribe(function(response) {
        if (response.error === 'NotConnectedToWifi') {
         //Show alert
        } else if (response.ok) {
            //Display response in UI
        } else {
            //Show alert showing error
        }
    });

细胞高度

class FriendsFeedTableViewCell: UITableViewCell, UIScrollViewDelegate {

@IBOutlet weak var imageScrollView: UIScrollView!
@IBOutlet weak var pageControl: UIPageControl!

func scrollViewDidEndDecelerating(scrollView: UIScrollView) { //current page: works
    let currentPage = (CGFloat(imageScrollView.contentOffset.x) / CGFloat(userImageView!.frame.size.width))
    pageControl.currentPage = Int(currentPage)
}
}

TableViewController:

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return 470
}

在我的TableViewViewController中我使用的按钮一旦点击它就会触发preforemSegueWithIdentifier

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

let cell: FriendsFeedTableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as! FriendsFeedTableViewCell

//Grabbing array of images from master Image Array
//images array will vary 1-3: as of now
let images = masterImageArray[indexPath.row]

//ScrollView setup
let scrollView = cell.imageScrollView
scrollView.pagingEnabled = true
scrollView.scrollEnabled = true
scrollView.tag = indexPath.row

//setup the image array for scrollView
var imageViews = [UIImageView]()
imageViews.append(cell.imageView_1)
imageViews.append(cell.imageView_2)
imageViews.append(cell.imageView_3)

//hidding all ImageViews: will only show if needed
cell.imageView_1.hidden = true
cell.imageView_2.hidden = true
cell.imageView_3.hidden = true


//tells how many pages will be need
for var i = 0; i < images.count; i++ {
    imageViews[i].image = images[i]
    scrollView.contentSize = CGSize(width: 310 * (i + 1), height: 400)
    cell.pageControl.numberOfPages = (i + 1) // tells how many pages
    imageViews[i].hidden = false
}

//button: Tells which image in indexPath we want to view
cell.tapToViewButton.tag = indexPath.row
cell.tapToViewButton.addTarget(self, action: "buttonPressed:", forControlEvents: .TouchUpInside)

return cell
}

我无法弄清楚这一点。我搜索但是每个人都在使用“didSelectRowAtIndexPath”,我没有使用它。

修改:----------------

我试图同时包含@ozgur和@Mundi的推荐,但它没有用。

func buttonPressed(sender: UIButton){
    imageSenderTag = sender.tag        
    self.performSegueWithIdentifier("toViewImageVC", sender: self)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if (segue.identifier == "toViewImageVC"){
        let viewImageVC: ViewImageViewController = segue.destinationViewController as! ViewImageViewController

        //moves [image] to next VC
        viewImageVC.imageArrayToPass = masterImageArray[imageSenderTag]
        //FIXME: need to pass "currentPage" (Int) to newViewController
    }
}

试过:@ozgur推荐

func buttonPressed(sender: UIButton){
    self.performSegueWithIdentifier("toViewImageVC", sender: sender)
}

错误:“UITableViewCell”类型的值没有成员'pageControl'

UITableViewCell,肯定有一个pageControl。为什么会发生这种情况?它正在引用那些单元格信息,对吗?

TRIED:@Mundi的推荐

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if (segue.identifier == "toViewImageVC") {
        let viewImageVC: ViewImageViewController = segue.destinationViewController as! ViewImageViewController
        viewImageVC.mainUserImageToPass = masterUserPicsArray[imageSenderTag]
        viewImageVC.imageArrayToPass = masterImageArray[imageSenderTag]

        if let button = sender as? UIButton {
            let indexPath = NSIndexPath(forRow: button.tag, inSection: 0)
            let cell = resultsTable.cellForRowAtIndexPath(indexPath)

                  //ERROR
            ***viewImageVC.pageNumber = cell!.pageControl.currentPage***
        }

    }
}

发送到ViewImageViewController的pageNumber发送单元格的indexPath.row而不是indexPath.row中的UIScrollView的当前页面

2 个答案:

答案 0 :(得分:1)

假设您的表格视图中只有一个部分;

首先,您可以在buttonPressed:方法中将UIButton作为segue的发送者传递:

func buttonPressed(sender: UIButton) {    
  self.performSegueWithIdentifier("toViewImageVC", sender: sender)
}

其次,您可以使用tag方法中的prepareForSegue值获取该按钮所属的单元格:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
  if let button = sender as? UIButton {
    let indexPath = NSIndexPath(forRow: button.tag, inSection: 0)
    let cell = tableView.cellForRowAtIndexPath(indexPath) as! FriendsFeedTableViewCell
    ...
    viewImageVC.currentPage = cell!.pageControl.currentPage
  }
}

如果您有多个部分;您可以获取@Mundi所描述的indexPath

let point = button.convertPoint(CGPointZero, toView: tableView)
let indexPath = tableView.indexPathForRowAtPoint(point)!

答案 1 :(得分:0)

致电self.performSegueWithIdentifier时,请传递sender: sender。确保将tag设置为当前页面。

prepareForSegue中,您现在可以访问Int,如下所示:

let button = sender as! UIButton
let point = button.convertPoint(CGPointZero, toView: self.tableView)
let indexPath = self.tableView.indexPathForRowAtPoint(point)!
let idx = indexPath.row // use this for accessing your image view arrays
viewImageVC.currentPage = sender.tag