UItableView的使用

前端之家收集整理的这篇文章主要介绍了UItableView的使用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

import UIKit

import Foundation


class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {


let data = NSArray(objects: "a","b","c")

override func viewDidLoad() {

super.viewDidLoad()

// imageView.image=UIImage(named: "3");

// let myView = UIView(frame: CGRectMake(10,30,355,400));

// myView.backgroundColor=UIColor.redColor()

// self.view.addSubview(myView)

// addBtn()

// addTf()

// addTextView()

// addMenu()

// addUIWebView()

addtableView()

}

func addtableView()

{

let tableView=UITableView(frame: CGRectMake(0,20,300,400))

tableView.delegate? = self

tableView.dataSource = self

self.view.addSubview(tableView)

}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {

return 1

}

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

return data.count

}

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

let cell = UITableViewCell(style: .Default,reuseIdentifier: "reuseIdentifier")

cell.textLabel!.text = "\(data.objectAtIndex(indexPath.row))"


return cell

}


let textView = UITextView(frame: CGRectMake(100,100,200))

func addTextView()

{

textView.textColor=UIColor.blueColor()

textView.text="1234455"

self.view.addSubview(textView)

textView.becomeFirstResponder()

}

func addMenu()

{

let myMenuItem = UIMenuItem(title: "突出",action:("close"))

let myMenu = UIMenuController.sharedMenuController()

myMenu.menuItems = NSArray(object: myMenuItem) as? [UIMenuItem]

}

func close()

{

textView.resignFirstResponder()

}

let btn = UIButton(frame: CGRectMake(0,0,73,44))

func addBtn()

{

let loadBtn = UIButton(type: UIButtonType.System)

let backBtn = UIButton(type: UIButtonType.System)

loadBtn.frame=CGRectMake(10,50,30)

backBtn.frame=CGRectMake(200,30)

loadBtn.setTitle("load",forState:UIControlState.Normal)

backBtn.setTitle("back",forState:UIControlState.Normal)

textUrlLel.text="http://www.baidu.com"

loadBtn.addTarget(self,action: "loginAction:",forControlEvents: UIControlEvents.TouchUpInside)

backBtn.addTarget(self,action: "backAction:",forControlEvents: UIControlEvents.TouchUpInside)

self.view.addSubview(webView)

self.view.addSubview(loadBtn)

self.view.addSubview(backBtn)

self.view.addSubview(textUrlLel)


}

var curUrl="http://www.baidu.com"

func loginAction(button: UIButton)

{

url=NSURL(string: textUrlLel.text!)

let request=NSURLRequest(URL:url!);

webView.loadRequest(request)

}

func backAction(button: UIButton)

{

url=NSURL(string: curUrl)

let request=NSURLRequest(URL: url!);

webView.loadRequest(request)

}

func addTf()

{

let tf=UITextField(frame: CGRectMake(50,50))

tf.textColor=UIColor.blueColor()

tf.text="tfTest"

self.view.addSubview(tf)

}

var textUrlLel=UITextField(frame: CGRectMake(80,30))

var url=NSURL(string: "http://www.baidu.com")

let webView=UIWebView(frame: CGRectMake(0,500,600))

func addUIWebView()

{


// let html="this is <font color=blue>good</font><font size=12>haode</font>"

// webView.loadHTMLString(html,baseURL: nil)

}

func flatButtonPressed()

{

print("abc")

url = NSURL(string: textUrlLel.text!)

print(textUrlLel.text)

let request = NSURLRequest(URL: url!)

webView.loadRequest(request)

}

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {

return UIInterfaceOrientationMask.Portrait

}

override func shouldAutorotate() -> Bool {

return true

}

override func viewWillTransitionToSize(size: CGSize,withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {

super.viewWillTransitionToSize(size,withTransitionCoordinator: coordinator)

let orientation = UIDevice.currentDevice().orientation

if(orientation == UIDeviceOrientation.Portrait)

{

btn.frame = CGRectMake(10,10,44)

}

else

{

btn.frame = CGRectMake(0,44)

}

}

override func setEditing(editing: Bool,animated: Bool) {

}


override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

}



}

猜你在找的Swift相关文章