iOS fifth lesson--gesture and TableView

Source: Internet
Author: User

This time we will learn gesture, TableView, alertview three kinds of technology.

First, Gesture

In iOS, you can use the system's built-in gesture recognition (Gesturerecognizer), or you can create your own gestures.

Gesturerecognizer converts a low-level transition to a higher execution behavior and then binds to the view object, so when a gesture occurs, the bound view object responds and determines whether the action corresponds to a specific gesture (Swipe,pinch,pan, Rotation

If it can recognize a gesture, it sends a message to the view that binds it.

The Uikit framework provides predefined gesturerecognizer:uitapgesturerecognizer, Uipangesturerecognizer, Uipinchgesturerecognizer, Uiswipegesturerecognizer, Uirotationgesturerecognizer, Uilongpressgesturerecognizer.

Adding gestures takes only three steps:

    1. Create a gesture recognizer (such as a UITapGestureRecognizer) instance, set the target (typically self) in its AddTarget method, and the action method, specifying properties such as the identifier of the recognizer instance Isuserinteractionenable = True
    2. Additional recognizers to view: *view.addgesturerecognizer (recognizer instance)
    3. Implement the method specified by the action

Full code:

Import UIKit

Class Viewcontroller:uiviewcontroller {

@IBOutlet weak var tapview:uiview!

var lastrotation = CGFloat ()

Let Taprec = UITapGestureRecognizer ()

Override Func Viewdidload () {

Super.viewdidload ()

Taprec.addtarget (Self, Action: #selector (Viewcontroller.tappedview))

Tapview.addgesturerecognizer (TAPREC)

Tapview.isuserinteractionenabled = True

}

Func Tappedview ()

{

Print ("Hello")

}

}

Second, UITableView

Table is one of the most common UI controls for displaying data. Data display style: List style plain, block style grouped.

TableView implementation steps:

    1. Add UITableView and prototype Cells.
    2. A class is required to follow the Uitableviewdelegate, Uitableviewdatasource protocol (and implement three specific methods later), and then set the proxy for TableView to the class just now: Tableview.delegate = Self (or other class name), Tableview.datasource = self.
    3. Set the identifier of the Tableviewcell (as long as it is unique)
    4. Initializing data
    5. Implement the three specific, required methods of the Uitableviewdatasource protocol to achieve data display in these three methods: set the number of sections, the number of rows in a single section, and display cell data.

Here is an example of TableView:

Import UIKit

Class Viewcontroller:uiviewcontroller,uitableviewdelegate,uitableviewdatasource {

var myName = [String] ()

@IBOutlet weak var txtname:uitextfield!

@IBOutlet weak var tvwnames:uitableview!

Override Func Viewdidload () {

Super.viewdidload ()

Tvwnames.delegate = self// 2nd step

Tvwnames.datasource = Self

Myname.append ("AA")//initial default value

}

@IBAction Func Btn_add (_ Sender:uibutton) {

If let name = txtName.Text {

Myname.append (name)

Tvwnames.reloaddata ()

}

}

@IBAction Func Btn_save (_ Sender:uibutton) {

}

@IBAction Func Btn_read (_ Sender:uibutton) {

}

Func numberofsections (in Tableview:uitableview), Int {

Return 1

}

The number of rows within a single section

Func TableView (_ Tableview:uitableview, Numberofrowsinsection section:int), Int {

Return Myname.count

}

Func TableView (_ Tableview:uitableview, Cellforrowat indexpath:indexpath), UITableViewCell {

Let Cellidentifier = "MyName"

Let cell = Tableview.dequeuereusablecell (Withidentifier:cellidentifier, For:indexpath)// 4th step Setting data

Cell.textlabel?. Text = Myname[indexpath.row]//Set initial data

return cell//returns data

}

}

TableView can also customize the table: Develop a display template for each row (cell).

Alert Actionview:

iOS has two kinds of pop-up prompts: Alert (on-screen popup), Actionsheet (bottom popup menu).

Put a button first, then right button to drag a @ibaction method, then ...

Full code:

@IBAction func btnexit (Sender:uibutton) {
Let Alertcontroller = Uialertcontroller (title: "caption", message: "This is what I want to show", Preferredstyle:.Alert)//Indicates a frame alert, or it can be a actionsheet
Let okaction = uialertaction (title: "Good", Style:.) Default, Handler: {
Action in
Print ("Hello")
Exit (0)
})
Let cancelaction = uialertaction (title: "Cancel", Style:.) Cancel, Handler:nil)//can be used. Destructive warning: text color turns red
Alertcontroller.addaction (cancelaction)
Alertcontroller.addaction (okaction)
Self.presentviewcontroller (Alertcontroller, Animated:true, Completion:nil)

Some are self.present (Alertcontroller, Animated:true, Completion:nil), in short a pattern box

}

Second Exam complete code : Files.cnblogs.com/files/quanxi/2014110231.zip

files.cnblogs.com/files/quanxi/latest. zip

iOS fifth lesson--gesture and TableView

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.