Developing Mac Apps with Swift (5)

Source: Internet
Author: User

Show insect Details

Open Masterviewcontroller.swift Add the following method:

func selectedbugdoc() -> Scarybugdoc? {

let Selectedrow = self. bugstableview.selectedrow;

if selectedrow >= 0 && selectedrow < Self. Bugs. Count {

return Self. Bugs[selectedrow]

}

return Nil

}

This method retrieves the response object from the data model based on the row index selected by the user.

Then here's the method:

func updatedetailinfo(doc: scarybugdoc?) {

var title = ""

var image: nsimage?

var rating = 0.0

if let Scarybugdoc = doc {

Title = ScaryBugDoc.data.title

Image = scarybugdoc.fullimage

Rating = scaryBugDoc.data.rating

}

self. Bugtitleview.stringvalue = title

self. Bugimageview.image = image

self. bugrating.rating = Float(rating)

}

This method displays insect information and pictures on the UI based on the Scarybugdoc object. Then here's the method:

func tableviewselectiondidchange(notification: nsnotification!) {

let Selecteddoc = selectedbugdoc()

Updatedetailinfo(selecteddoc)

}

This method invokes the first two practical methods when the user changes the selection in the table.

Starting with OS X 10.10 Yosemite, the View Controller uses a new

viewwillappear, viewdidload, and other iOS-style life-cycle methods. The traditional method of creating views in OS X is generally loadview (), which is backwards compatible, so we use this method:

Override func loadview() {

Super. Loadview()

self. Bugrating.starimage = nsimage(named: "Star.png" )

self. Bugrating.starhighlightedimage = nsimage(named: " Shockedface2_full.png ")

self. Bugrating.starimage = nsimage(named: "shockedface2_ Empty.png ")

self. Bugrating.delegate = Self

self. bugrating.maxrating = 5

self. Bugrating.horizontalmargin =

self. bugrating.editable = true

self. Bugrating.displaymode = UInt(edstarratingdisplayfull)

self. bugrating.rating = Float(0.0)

}

Here, we initialize the Edstarrating control: The image used to represent the insect thriller Index, the delegate property of the control, and other parameters.

Then add a extension statement at the end of the Masterviewcontroller.swift :

//MARK:-Edstarratingprotocol

Extension Masterviewcontroller: edstarratingprotocol { }

Wait here to implement this Edstarratingprotocol protocol.

Compile and run the program first, the effect is as follows:


Add , Delete

Open Masterviewcontroller.xib and drag two "Gradient button" to the table view. Select one of the buttons, open the Attributes panel, delete the contents of the Title property, and then select it in the Image property, which will cause the button to appear as a "+" sign.

Similarly, set the other button to the "-" sign button (the Image property is selected as "Nsremovetemplate").


Open the Assistant Editor window and make sure the current content is Masterviewcontroller.swift file, first add an extended definition:

//MARK:-Ibactions

Extension Masterviewcontroller { }

Strictly speaking, this extension is not required, but in this way we can better organize our swift code. Then select the Plus button and right-drag a line onto the extension.


In the pop-up window, connection a column, select Action,name, enter addbug, and click Connect.


This will create a addbug (_:) method, the system will call this method whenever the plus button is clicked. Repeat the same step on the minus button, name using deletebug.

Open the masterviewcontroller.swift implementation Addbug method as follows:

//1. Use the default value to create a new Scarybugdoc Example

let Newdoc = scarybugdoc(title: "New Bug", rating: 0.0 , Thumbimage: nil, fullimage: nil)

//2. Add the instance to the Model Array

self. Bugs.append(newdoc)

let Newrowindex = self. Bugs. Count - 1

//3. to Table View Insert new Row

self. Bugstableview.insertrowsatindexes(nsindexset(index: newrowindex), withanimation: nstableviewanimationoptions.effectgap)

//4. Select and scroll to a new line

self. Bugstableview.selectrowindexes(nsindexset(index: newrowindex ), byextendingselection:false)

self. bugstableview.scrollrowtovisible(newrowindex)

The implementation of the Deletebug () method is as follows:

//1. Get Selected Doc

if let Selecteddoc = selectedbugdoc() {

//2. Remove the bug from the model

Self-bugs.removeatindex(self bugstableview.selectedrow)

//3. Remove the selected row from the table view

self. bugstableview.removerowsatindexes(

Nsindexset (index:self. Bugstableview.selectedrow),

Withanimation: nstableviewanimationoptions.slideright)

//4. Clear Detail Info

Updatedetailinfo(nil)

}


Edit

Open masterviewcontroller.xib, open Assistant Editor, and make sure that the file currently displayed is Masterviewcontroller.swift.

Select the text field and right-drag to the Addbug () method in the masterviewcontroller.swift file:


This will allow you to create a ibaction,name for the text field, using bugtitledidendedit.


This method will be called when the text field finishes editing (when the user presses the ENTER key or leaves the Text field control).

Back to masterviewcontroller.swift, Add method:

func reloadselectedbugrow() {

let Indexset = nsindexset(index: self. Bugstableview.selectedrow)

let ColumnSet = nsindexset(index: 0)

self. Bugstableview.reloaddataforrowindexes(indexset, columnindexes: columnset)

}

In this method, we reload the row data model, and you need to call this method after the model data has been altered.

The Bugtitledidendedit method is implemented as follows:

if let Selecteddoc = selectedbugdoc() {

SelectedDoc.data.title = self. bugtitleview.stringvalue

Reloadselectedbugrow()

}

First, call Selectedbugdoc () to get information about the insect, and then read the text string from its text field and use it to update the name of the insect in the model. Finally, call the Reloadselectedbugrow () notification cell to refresh.

Note : It is better to notify table view to refresh the cell itself than directly manipulate the contents of the cell.

Run the app, select an insect from the list, try to change its name (remember to press ENTER), and the insect names in the table change!

But if you switch to another insect and then return to the modified one, you will find the data back to the original (unchanged). This is because we have not persisted the model object (saved into the file).



Developing Mac Apps with Swift (5)

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.