import UIKit
class ViewController: UIViewController,NSURLConnectionDataDelegate,UITableViewDelegate,UITableViewDataSource {
var listData = ["开始游戏","网络请求","tableView"]
@IBOutlet var newsTableView: UITableView!
var refreshControl = UIRefreshControl()
var dataArray:[AnyObject] = [AnyObject]()
var messageData = NSDictionary()
override func viewDidLoad() {
super.viewDidLoad()
//添加刷新
refreshControl.addTarget(self, action: "refreshData", forControlEvents: UIControlEvents.ValueChanged)
refreshControl.attributedTitle = NSAttributedString(string: "松开后自动刷新")
newsTableView.addSubview(refreshControl)
refreshData()
}
// 刷新数据
func refreshData() {
self.newsTableView.reloadData()
self.refreshControl.endRefreshing()
}
//分组个数
func numberOfRowsInSection(section: Int) -> Int{
return 1;
}
//cell 个数
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print(listData.count);
return listData.count;
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIndenttifier:String = "CellIndenttifier";
let cell:UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: cellIndenttifier);
cell.textLabel?.text = listData[indexPath.row]
return cell
}
//确定选择的是某一个
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
let alertControler = UIAlertController(title: "提示", message:listData[indexPath.row], preferredStyle: UIAlertControllerStyle.Alert)
if(alertControler.message == "开始游戏"){
alertControler.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: {
action in
self.presentViewController(MainTableViewController(), animated: true, completion: nil)
}))
self.presentViewController(alertControler, animated: true, completion: nil)
}
if(alertControler.message == "网络请求"){
getMessage()
}
if(alertControler.message == "tableView"){
let mainStoryboard = self.storyboard
let myTableView = (mainStoryboard?.instantiateViewControllerWithIdentifier("MyTableView"))
self.navigationController?.pushViewController(myTableView!, animated: true)
}
}
func getMessage(){
var userid : Int!
userid=412
let urlString = "http://www.didiweihuo.com/wcar/phone_getUserInfo.action"
var url : NSURL!
url = NSURL(string: urlString)
let request = NSMutableURLRequest(URL: url)
let body = "userid=\(userid)"
let postData = body.dataUsingEncoding(NSASCIIStringEncoding)
request.HTTPBody = postData
request.HTTPMethod = "post"
var conn : NSURLConnection!
conn = NSURLConnection(request: request, delegate: self)
conn.start()
}
func connection(connection: NSURLConnection, didReceiveResponse response: NSURLResponse) {
print("请求成功!")
}
func connection(connection: NSURLConnection, didReceiveData data: NSData){
let json : AnyObject! = try? NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments)
let result = json.valueForKey("data") as! NSArray
let nameResult = result.objectAtIndex(0)
let name = nameResult.valueForKey("name") as? String
print(result)
print(name)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}