Xcode 6.3与Swift崩溃与tableview

时间:2015-03-03 18:32:13

标签: ios xcode uitableview swift xcode6

我在app delegate中设置UItabbar后出现此问题。

问题是:

  1. 只有在按下发现标签栏后,应用才会崩溃。
  2. Home + Add + Profile ViewControllers是Black。
  3. 提前谢谢

    var discoveryList = ["topic","topic","topic","topic","topic"]    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // println(PFUser.currentUser())
    
        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false
    
        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem()
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    // MARK: - Table view data source
    
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Potentially incomplete method implementation.
        // Return the number of sections.
        return 10
    }
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete method implementation.
        // Return the number of rows in the section.
        return discoveryList.count
    }
    
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell
    
    
        cell.textLabel?.text = discoveryList[indexPath.row]
        cell.textLabel?.text?.capitalizedString
        cell.textLabel?.textAlignment = NSTextAlignment.Center
    
    
    
        // Configure the cell...
    
        return cell
    }
    // App Delegate
    
     // Create UIWindow within the screen boundaries.
        self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
    
        var nav1 = UINavigationController()
        var first = ccHome(nibName: nil, bundle: nil)
        nav1.viewControllers = [first]
        nav1.tabBarItem = UITabBarItem(title: "Home", image: UIImage(named:"house28.png"), tag: 1)
    
        var nav2 = UINavigationController()
        var second = ccDiscovery(nibName: nil, bundle: nil)
        nav2.viewControllers = [second]
        nav2.tabBarItem = UITabBarItem(title: "Discovery", image: UIImage(named:"three49.png"), tag: 1)
    
        var nav3 = UINavigationController()
        var third = ccPosting(nibName: nil, bundle: nil)
        nav3.viewControllers = [third]
        nav3.tabBarItem = UITabBarItem(title: "Add", image: UIImage(named:"add182.png"), tag: 1)
    
        var nav4 = UINavigationController()
        var four = ccNotification(nibName: nil, bundle: nil)
        nav4.viewControllers = [four]
        nav4.tabBarItem = UITabBarItem(title: "Notification", image: UIImage(named:"connection27.png"), tag: 1)
    
        var nav5 = UINavigationController()
        var five = ccProfile(nibName: nil, bundle: nil)
        nav5.viewControllers = [five]
        nav5.tabBarItem = UITabBarItem(title: "Profile", image: UIImage(named:"male12.png"), tag: 1)
    
        var tabs = UITabBarController()
        tabs.viewControllers = [nav1, nav2, nav3,nav4,nav5]
    
        self.window!.rootViewController = tabs;
        self.window?.makeKeyAndVisible();
    

1 个答案:

答案 0 :(得分:-1)

我认为问题在于你用来获取UITableViewCell的代码。

你在做......

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell


    cell.textLabel?.text = discoveryList[indexPath.row]
    cell.textLabel?.text?.capitalizedString
    cell.textLabel?.textAlignment = NSTextAlignment.Center



    // Configure the cell...

    return cell
}

这仅重用现有的UITableViewCell,但不会创建一个func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as? UITableViewCell; if (cell == nil) { cell = SavedAppraisalTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: cellIdentifier); } // do some stuff return cell!; } 。您需要添加一个条件来实例化新的UITableViewCell(如果尚不存在)。

{{1}}