我现在的表格视图是"动态原型"所以基本上我不能通过增加像静态单元格的部分来添加部分。但我喜欢保持动态原型。那么我究竟能创建一个包含自己单元格的部分呢?
这是我的代码:
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 :(得分:1)
通过这种方式,您可以在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"
}
}