2016-09-07 30 views
-3

我的表格視圖現在是在「動態原型」,所以基本上我不能通過增加節像靜態單元格一樣添加節。但我喜歡保持動態的原型。那麼,我怎樣才能創建一個包含他們自己的細胞的部分?如何用剖面創建表格視圖?

這裏是我的代碼:

class ProfileViewController: BaseViewController, UITableViewDelegate, UITableViewDataSource,FBSDKLoginButtonDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate { 

    @IBOutlet weak var profileTableView: UITableView! 
    @IBOutlet weak var profileImg: UIImageView! 
    @IBOutlet weak var bigIDLbl: UILabel! 
    @IBOutlet weak var userNameLbl: UILabel! 

    let titleArr = ["My Profile", "My BIG Point", "Options"] 
    var detailArr = [String]() 

    let userInfo = defaults.objectForKey("userInfo") 
    let userTicketId = defaults.objectForKey("userticketId") as! String 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     //setupWhiteLeftButton() 
     self.navigationItem.title = "My Profile" 

     bigIDLbl.text = "BIG ID : \(userInfo!["CustomerNumber"] as! String)" 
     userNameLbl.text = "\((userInfo!["FirstName"] as! String).capitalizedString) \((userInfo!["LastName"] as! String).capitalizedString)" 
     detailArr = ["\((userInfo!["FirstName"] as! String).capitalizedString) \((userInfo!["LastName"] as! String).capitalizedString)", "8,950pts", ""] 

     let username = userInfo!["UserName"] as! String 
     let password = defaults.objectForKey("userPassword") as! String 

     AirAsiaBigProvider.request(.GetUser(username, password, userTicketId), completion: { (result) in 
      switch result { 
      case .Success(let successResult): 
       do { 

        let json = try JSON(NSJSONSerialization.JSONObjectWithData(successResult.data, options: .MutableContainers)) 

        if json["Status"].string == "OK" { 
         defaults.setValue(json.object, forKey: "userData") 
        } else if json["Status"].string == "Error" { 
         showErrorMessage(json["Message"].string!) 
        } 

       } 
       catch { 
        showErrorMessage("Unable to connect the server") 
       } 
      case .Failure(let failureResult): 
       showErrorMessage(failureResult.nsError.localizedDescription) 
      } 
     }) 



     // Do any additional setup after loading the view. 
    } 

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

    @IBAction func profileImgPressed(sender: AnyObject) { 
     let alert = UIAlertController(title: nil, message: nil, preferredStyle: .ActionSheet) 


     let choosePhoto = UIAlertAction(title: "Choose From Photos", style: .Default, handler: { 
      (alert: UIAlertAction!) -> Void in 
      NSLog("Action 1 Clicked") 

      if UIImagePickerController.isSourceTypeAvailable(.PhotoLibrary) { 
       let imagePicker = UIImagePickerController() 
       imagePicker.allowsEditing = true 
       imagePicker.delegate = self 
       imagePicker.sourceType = .PhotoLibrary 
       self.presentViewController(imagePicker, animated: true, completion: nil) 
      } else { 
       showErrorMessage("Error Launch Photos") 
      } 
     }) 

     let takePhoto = UIAlertAction(title: "Take Photo", style: .Default, handler: { 
      (alert: UIAlertAction!) -> Void in 
      NSLog("Action 2 Clicked") 

      if UIImagePickerController.isSourceTypeAvailable(.Camera) { 
       let imagePicker = UIImagePickerController() 
       imagePicker.allowsEditing = true 
       imagePicker.delegate = self 
       imagePicker.sourceType = .Camera 
       self.presentViewController(imagePicker, animated: true, completion: nil) 
      } else { 
       showErrorMessage("Error Taking Photo") 
      } 
     }) 

     let deletePhoto = UIAlertAction(title: "Delete Photo", style: .Default, handler: { 
      (alert: UIAlertAction!) -> Void in 
      NSLog("Action 3 Clicked") 
      self.profileImg.image = UIImage(named: "profileImg") 
     }) 

     let cancel = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil) 

     alert.addAction(choosePhoto) 
     alert.addAction(takePhoto) 
     alert.addAction(deletePhoto) 
     alert.addAction(cancel) 
     presentViewController(alert, animated: true, completion: nil) 
    } 

    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { 
     profileImg.image = info[UIImagePickerControllerOriginalImage] as? UIImage 
     profileImg.contentMode = UIViewContentMode.ScaleAspectFill 
     profileImg.clipsToBounds = true 
     dismissViewControllerAnimated(true, completion: nil) 
    } 

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return titleArr.count 
    } 

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

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

     let cell = profileTableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CustomProfileTableViewCell 
     cell.title.text = titleArr[indexPath.row] 
     cell.detail.text = detailArr[indexPath.row] 
     cell.layoutMargins = UIEdgeInsetsZero 
     return cell 

    } 

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 


     if indexPath.row == 0 { 

      let storyboard2 = UIStoryboard(name: "Profile", bundle: nil) 
      let myProfileVC = storyboard2.instantiateViewControllerWithIdentifier("ViewProfileVC") as! myProfileTableViewController 
      self.navigationController?.pushViewController(myProfileVC, animated: true) 
     } else if indexPath.row == 2{ 

      let storyboard2 = UIStoryboard(name: "Profile", bundle: nil) 
      let profileVC = storyboard2.instantiateViewControllerWithIdentifier("OptionsVC") as! OptionsViewController 
      self.navigationController?.pushViewController(profileVC, animated: true) 
     } 

    } 

    func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) { 
     print("User Login") 
    } 
    func loginButtonDidLogOut(loginButton: FBSDKLoginButton!) { 

     print("User Logged Out") 
    } 

    /* 
    // 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. 
    } 
    */ 

} 
+0

使用FUNC numberOfSectionsInTableView(的tableView:UITableView的) - >詮釋{ 返回1 //數你想在你的桌面視圖部分 }不要忘記在你的視圖控制器中添加UITableViewDataSource。 – Tuhin

+0

[UITableView與不同的可選部分?]可能重複?(http://stackoverflow.com/questions/31741035/uitableview-with-different-optional-sections) –

+0

明白了。但它在我的第一行重複。我該如何編輯該部分的標題? –

回答

3

通過這種方式,您可以在您的tableView添加部分

override func viewDidLoad() { 

     super.viewDidLoad() 
     profileTableView.delegate = self 
     profileTableView.dataSource = self 
    } 


func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     return 2 // As per your requirement you can return no of sections. 
    } 

    func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String 
    { 
     if section == 0 
     { 
      return "Section A" 
     } 
     else 
     { 
      return "Section B" 
     } 
    }