The UITableView control uses
Using UITableView, in the control library, drag a table view into the Viewcontroller, The protocol for Uitableviewdelegate and Uitableviewdatasource needs to be inherited in the controller's background code.
overriding method
TableView (_:numberofrowsinsection)
This method is a method of the Uitableviewdatasource protocol that returns the number of rows loaded by TableView.
Instance Code
Func TableView (Tableview:uitableview, Numberofrowsinsection section:int), Int {
Return the number of the rows in the section.
Returns the number of rows per node in a table, which can also be used in TableView, which is a group. Usage of subsequent introduction groups
Return Restaurantnames.count
}
TableView (_:cellforrowatindexpath)
This method is a method of the Uitableviewdatsource protocol that implements how the data in the TableView is loaded.
Instance Code
Func TableView (Tableview:uitableview, Cellforrowatindexpath Indexpath:nsindexpath)->uitableviewcell {
Here is the cell is the Tableviewcell set in storyboard identifier
Let Cellidentifier = "Cell"
This is the Get cell
Let cell = Tableview.dequeuereusablecellwithidentifier (Cellidentifier, Forindexpath:
Indexpath) as UITableViewCell
Configure the cell ...
Cell.textLabel.text = Restaurantnames[indexpath.row]
return cell
}
Dequeuereusablecellwithidentifier: Retrieve a cell from the queue that has a name of [identifier] available. Why this is taken from the queue: see the following explanation:
Set DataSource and delegate
Two ways to set TableView DataSource and delegate
- Right-click in storyboard to select TableView
Drag datasource and delegate to the current Viewcontroller
- In the controller's code, the link TableView
@IBOutlet var tableview:uitableview!
Then set TableView DataSource and delegate in the Viewdidload method.
Tableview.datasource = Self
Tableview.delegate = Self
Set a cell's thumbnail image
In the method of TableView (_:cellforrowatindexpath)
Cell.imageView.image = UIImage (name: "imagename")
Hide the status bar
Override Func Prefersstatusbarhidden (), Bool {
return True
}
Customize cell Modify Custom property
In storyboard, select Tableviewcell, and in the property indexer, change the property of the style to custom
Modify TableView Line Height
Modify cell Row Height
Custom cell Styles
In the control library, you can drag and drop a control into a cell to see the format you want.
Create a class file for a custom cell
Add a new file (Command + N) to the project, select Cocoa Touch Class, select UITableViewCell in subclass of, no Xib file required.
To define a control in a class file
@IBOutlet weak var namelabel:uilabel!
@IBOutlet weak var locationlabel:uilabel!
@IBOutlet weak var typelabel:uilabel!
@IBOutlet weak var thumbnailimageview:uiimageview!
Association class files and control definitions
Set the Tableviewcell class to the new class
To set a control association
Post-Association results
To modify the method that gets cells
In the TableView (_:cellforrowatindexpath) method, the original method of getting the cell is modified
Let cell = Tableview.dequeuereusablecellwithidentifier (Cellidentifier, Forindexpath:
Indexpath) as UITableViewCell
After modification
Let cell = Tableview.dequeuereusablecellwithidentifier (Cellidentifier, Forindexpath:
Indexpath) as Customtableviewcell
Customtableviewcell is our newly defined class.
Set cells
Cell.nameLabel.text = Restaurantnames[indexpath.row]
Cell.thumbnailImageView.image = UIImage (Named:restaurantimages[indexpath.row])
How to set a picture fillet
Code implementation:
Cell.thumbnailImageView.layer.cornerRadius = CELL.THUMBNAILIMAGEVIEW.FRAME.SIZE.WIDTH/2
Cell.thumbnailImageView.clipsToBounds = True
Clipstobounds is a property switch, only open, rounded corner setting is valid
Setup implementation:
Select the ImageView control and make the following settings
Add the key and value in the user Defined Runtime attributes. You do not need to set the switch.
Again, we can modify the Backgroundcolor,fond here ...
Cell Selection and Uialertcontroller
In the Uitableviewdelegate protocol, there are two ways
TableView (_:willselectrowatindexpath) before selecting
TableView (_:didselectrowatindexpath) after selection
Set cell selection
Let cell = Tableview.cellforrowatindexpath (Indexpath)
Through the Indexpath to get a cell, there will be a bug, before we have said that the cell load is obtained through the queue, the impression of the queue, in the acquisition of the cell, there will be a misplaced bug, the solution to this problem is through the control data source to solve.
Set the cell's selected state by setting the cell's Accessorytype
Cell.accessorytype = Uitableviewcellaccessorytype.checkmark
Use of Uialertcontroller
Example code we write in the TableView (_:didselectrowatindexpath) method
Override Func TableView (Tableview:uitableview, Didselectrowatindexpath Indexpath:
Nsindexpath) {
Create a option menu as an action sheet
Let Optionmenu = Uialertcontroller (title:nil, message: "What does want to do?",
Preferredstyle:. Actionsheet)
ADD Actions to the menu
Let cancelaction = uialertaction (title: "Cancel", Style:.) Cancel, Handler:nil)
Optionmenu.addaction (cancelaction)
Display the Menu
Self.presentviewcontroller (Optionmenu, Animated:true, Completion:nil)
}
There are two types of Uialertcontrollerstyle,
. Alert
. Actionsheet
1, use Alertcontroller First is to create Uialertcontroller object
Let Optionmenu = Uialertcontroller (title:nil, message: "What does want to do?",
Preferredstyle:. Actionsheet)
2. Then create Uialertaction
Let cancelaction = uialertaction (title: "Cancel", Style:.) Cancel, Handler:nil)
3. Add action to the controller, if Alertcontrollerstyole is. Actionsheet, you can add more than one action in Alertcontroller
Optionmenu.addaction (cancelaction)
4. Present Alertcontroller
Display the Menu
Self.presentviewcontroller (Optionmenu, Animated:true, Completion:nil)
In the Create action, about handler, here is a delegate that can be implemented with closures, or a function transfer function name, which is the delegate that fires after the action is clicked
Let isvisitedaction = uialertaction (title: "I ' ve Beenhere", Style:. Default, Handler: {
(action:uialertaction!) -Void in
Let cell = Tableview.cellforrowatindexpath (Indexpath)
Cell?. Accessorytype =. Checkmark
Self.restaurantisvisited[indexpath.row] = True
})
Simplifying closure notation
Let isvisitedaction = uialertaction (title: "I ' ve Beenhere", Style:. Default) {
$ A represents the first argument, where closing the closure usage can refer to the syntax note
Let sender = $
Let cell = Tableview.cellforrowatindexpath (Indexpath)
Cell?. Accessorytype =. Checkmark
Self.restaurantisvisited[indexpath.row] = True
}
Uialertview
Uialertview similar to action in Uialertcontroller, using relatively simple
Let Alertview = Uialertview (title: "", Message: "", Delegate:nil, Cancelbuttontitle: "")
Alertview.show ()
Deletion of TableRow
There's a way in the Uitableviewdatasource protocol.
TableView (_:commiteditingstyle:forrowatindexpath
Override the method in the Viewcontroller class to see the following effect without any implementation
Override Func TableView (Tableview:uitableview,
Commiteditingstyle Editingstyle:uitableviewcelleditingstyle,
Forrowatindexpath Indexpath:nsindexpath) {
}
When you click the Delete button, if you want to do something about it, you can do it in the above method
Instance code:
Override Func TableView (Tableview:uitableview, Commiteditingstyle Editingstyle:
Uitableviewcelleditingstyle, Forrowatindexpath Indexpath:nsindexpath) {
if Editingstyle = =. Delete {
Delete the row from the data source
Self.restaurantNames.removeAtIndex (Indexpath.row)
Self.restaurantLocations.removeAtIndex (Indexpath.row)
Self.restaurantTypes.removeAtIndex (Indexpath.row)
Self.restaurantIsVisited.removeAtIndex (Indexpath.row)
Self.restaurantImages.removeAtIndex (Indexpath.row)
Delete Row
Self.tableView.deleteRowsAtIndexPaths ([Indexpath], withrowanimation:. Fade)
}
Reload TableView
Self.tableView.reloadData ()
}
Add Rowaction
There's a new member Uitableviewrowaction in the iOS 8 SDK, and with this new member we can have more action on row
- One way to rewrite Uitableviewdatasource TableView (_:editactionsforrowatindexpath)
- Create Uitableviewrowaction
var shareaction = uitableviewrowaction (Style:UITableViewRowActionStyle.Default, title:
"Share", Handler:nil)
- Delegate after action is implemented
- Back to Rowaction
Instance code:
Override Func TableView (Tableview:uitableview,
Editactionsforrowatindexpath Indexpath:
Nsindexpath), [Anyobject] {
var shareaction = uitableviewrowaction (Style:UITableViewRowActionStyle.Default, title:
"Share", Handler: {(action:uitableviewrowaction!, indexpath:nsindexpath!), Void in
Let Sharemenu = Uialertcontroller (title:nil, message: "Share using",
Preferredstyle:. Actionsheet)
Let twitteraction = uialertaction (title: "Twitter", Style:
Uialertactionstyle.default, Handler:nil)
Let facebookaction = uialertaction (title: "Facebook", Style:
Uialertactionstyle.default, Handler:nil)
Let emailaction = uialertaction (title: "Email", Style:UIAlertActionStyle.Default,
Handler:nil)
Let cancelaction = uialertaction (title: "Cancel", Style:UIAlertActionStyle.Cancel,
Handler:nil)
Sharemenu.addaction (twitteraction)
Sharemenu.addaction (facebookaction)
Sharemenu.addaction (emailaction)
Sharemenu.addaction (cancelaction)
Self.presentviewcontroller (Sharemenu, Animated:true, Completion:nil)
}
)
var deleteaction = uitableviewrowaction (Style:UITableViewRowActionStyle.Default,
Title: "Delete", Handler: {(action:uitableviewrowaction!, indexpath:nsindexpath!), Void in
Delete the row from the data source
Self.restaurantNames.removeAtIndex (Indexpath.row)
Self.restaurantLocations.removeAtIndex (Indexpath.row)
Self.restaurantTypes.removeAtIndex (Indexpath.row)
Self.restaurantIsVisited.removeAtIndex (Indexpath.row)
Self.restaurantImages.removeAtIndex (Indexpath.row)
Self.tableView.deleteRowsAtIndexPaths ([Indexpath], withrowanimation:. Fade)
}
)
return [Deleteaction, ShareAction]
}
Beginning IOS 8 Programming with Swift-tableview