这里有一些可选的错误吗?

时间:2015-08-03 01:11:58

标签: ios swift

这是代码和问题

在添加函数tableView(...... didSelectRowatindexPath ...)之前选择一行时没有错误

所以,我认为这是根本原因。

我希望有人可以帮助我,因为错误的信息并不是那么清楚我能理解它。

enter image description here

当我选择弹出表的一行时,我想要做的是更改BarItemName。

SwitchA是我的popoverviewcontroller中的var,它表示按下了哪个按钮。

按下" SecondVC"按下,它会将值传递给SwitchA,然后popoverviewcontroller可以确定它应该显示哪个数据源。

enter image description here

PS:这是popoverviewcontroller的代码。

import UIKit

class PopOverView: UIViewController, UITableViewDataSource, UITableViewDelegate {



var SwitchA = 0
var ClassA = ["这个类型","那个类型","这个类型","那个类型","这个类型","那个类型","这个类型","那个类型","这个类型","那个类型"]
var TimeA = ["昨天","今天","明天","昨天","今天","明天"]
var TagA = ["动漫","音乐","游戏","音乐","游戏"]
@IBOutlet weak var TV: UITableView!
override func viewDidLoad() {
    super.viewDidLoad()


    TV.dataSource = self
    TV.delegate = self

    self.preferredContentSize = CGSize(width: UIScreen.mainScreen().bounds.width, height: 175)
    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
    if SwitchA == 0 {
        return ClassA.count
    }
    if SwitchA == 1 {
        return TimeA.count
    }
    else {
        return TagA.count
    }
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{

    if SwitchA == 0 {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell
        cell.textLabel?.text = ClassA[indexPath.row]
        cell.separatorInset = UIEdgeInsetsZero
        cell.preservesSuperviewLayoutMargins = false
        cell.layoutMargins = UIEdgeInsetsZero
        return cell
    }
    if SwitchA == 1 {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell
        cell.textLabel?.text = TimeA[indexPath.row]
        cell.separatorInset = UIEdgeInsetsZero
        cell.preservesSuperviewLayoutMargins = false
        cell.layoutMargins = UIEdgeInsetsZero
        return cell
    }
    else {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell
        cell.textLabel?.text = TagA[indexPath.row]
        cell.separatorInset = UIEdgeInsetsZero
        cell.preservesSuperviewLayoutMargins = false
        cell.layoutMargins = UIEdgeInsetsZero
        return cell
    }

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

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    tableView.deselectRowAtIndexPath(indexPath, animated: false)
    if SwitchA == 0 {
        let VC = self.storyboard!.instantiateViewControllerWithIdentifier("SecondVC") as! XiaoNei_HuoDong
        VC.ClassName.title = ClassA[indexPath.row]
    }
    if SwitchA == 1 {
        let VC = self.storyboard!.instantiateViewControllerWithIdentifier("SecondVC") as! XiaoNei_HuoDong
        VC.TimeName.title = TimeA[indexPath.row]
    }
    else {
        let VC = self.storyboard!.instantiateViewControllerWithIdentifier("SecondVC") as! XiaoNei_HuoDong
        VC.TagName.title = TagA[indexPath.row]
    }
}


/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
}
*/

}

这是" SecondVC"的代码:

import UIKit

class XiaoNei_HuoDong: UIViewController,UITableViewDelegate,UITableViewDataSource, UIPopoverPresentationControllerDelegate{

@IBOutlet weak var TagName: UIBarButtonItem!
@IBOutlet weak var TimeName: UIBarButtonItem!
@IBOutlet weak var ClassName: UIBarButtonItem!
@IBOutlet weak var huodongTV: UITableView!
override func viewDidLoad() {
    super.viewDidLoad()

    huodongTV.dataSource = self
    huodongTV.delegate = self

    huodongTV.showsVerticalScrollIndicator = false

    let options = PullToRefreshOption()
    options.backgroundColor = UIColor(red: 239/255, green: 239/255, blue: 244/255, alpha: 1)
    options.indicatorColor = UIColor.blackColor()

    huodongTV.addPullToRefresh(options: options, refreshCompletion: { [weak self] in
        // some code

        self!.huodongTV.reloadData()
        self!.huodongTV.stopPullToRefresh()
        })
    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
    return 3
}

func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 1.0
}
func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return 1.0
}
// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
    self.huodongTV.deselectRowAtIndexPath(indexPath, animated: false)
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
    if indexPath.row == 0 {
        let cell:HuoDong_2 = tableView.dequeueReusableCellWithIdentifier("huodong2") as! HuoDong_2
        cell.ClubB1.image = UIImage(named: "test")!
        cell.ClubB2.image = UIImage(named: "test")!
        cell.ClubS1.image = UIImage(named: "focus")!
        cell.ClubS2.image = UIImage(named: "focus")!
        cell.Tag1.image = UIImage(named: "更新")!
        cell.Tag2.image = UIImage(named: "更新")!
        cell.View1.image = UIImage(named: "view")!
        cell.View2.image = UIImage(named: "view")!
        cell.Newest.image = UIImage(named: "club rank")
        return cell
    }
    else {
        let cell:HuoDong = tableView.dequeueReusableCellWithIdentifier("huodong1") as! HuoDong
        cell.ClubB1.image = UIImage(named: "test")!
        cell.ClubB2.image = UIImage(named: "test")!
        cell.ClubS1.image = UIImage(named: "focus")!
        cell.ClubS2.image = UIImage(named: "focus")!
        cell.Tag1.image = UIImage(named: "更新")!
        cell.Tag2.image = UIImage(named: "更新")!
        cell.View1.image = UIImage(named: "view")!
        cell.View2.image = UIImage(named: "view")!
        return cell
    }
}

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath){
    cell.layer.transform = CATransform3DMakeScale(0.1, 0.1, 1)
    UIView.animateWithDuration(0.25, animations: {
        cell.layer.transform = CATransform3DMakeScale(1, 1, 1)
    })

}

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    if indexPath.row == 0 {
        return self.view.frame.width * 240.0 / 400.0
    }
    else {
        return self.view.frame.width * 200.0 / 400.0
    }
}




// MARK: - PopOverforsegue

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "Class1"{
        let VC = segue.destinationViewController as! PopOverView
        VC.SwitchA = 0
        VC.modalPresentationStyle = UIModalPresentationStyle.Popover
        VC.popoverPresentationController?.delegate = self
    }
    if segue.identifier == "Time1"{
        let VC = segue.destinationViewController as! PopOverView
        VC.SwitchA = 1
        VC.modalPresentationStyle = UIModalPresentationStyle.Popover
        VC.popoverPresentationController?.delegate = self
    }
    if segue.identifier == "Tag1"{
        let VC = segue.destinationViewController as! PopOverView
        VC.SwitchA = 2
        VC.modalPresentationStyle = UIModalPresentationStyle.Popover
        VC.popoverPresentationController?.delegate = self
    }
}

func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
    return UIModalPresentationStyle.None
}

/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
}
*/

}

1 个答案:

答案 0 :(得分:0)

我认为问题是初始化的时间 试试这个:

<强> SecondVC

var tagNameStr = ""

override func viewDidLoad() {
    super.viewDidLoad()
    TagName.title = tagNameStr  // here
    ...
}

<强> FirstVC

if SwitchA == 0 {
    let VC = self.storyboard!.instantiateViewControllerWithIdentifier("SecondVC") as! XiaoNei_HuoDong
    VC.tagNameStr = ClassA[indexPath.row]
}

希望这有帮助!

<强>更新

这是示例代码 (ViewController - | segue | - &gt; SecondViewController)

class ViewController: UIViewController {
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if let vc = segue.destinationViewController as? SecondViewController {
            vc.buttonTitle = "IOhYES"
        }
    }
}

class SecondViewController: UIViewController {
    @IBOutlet weak var buttonItem: UIBarButtonItem!
    var buttonTitle = ""

    override func viewDidLoad() {
        super.viewDidLoad()
        buttonItem.title = buttonTitle
    }
}

请检查IBOutlet连接。