Swift basics-use TableViewController to customize the table View
First, create a swift project, delete the storyboard content, add a Navigation Controller, set the class of the corresponding storyboard interface, and set the View Controller's is initial View Controller on the Navigation Controller Interface, the custom list content is used here. Therefore, you need to create a class that inherits UITableViewCell, and then set the class of Prototype Cells in Table View in storyboard. If you click item to go to the details page, use the prepareForSegue method in TableView.
JieTableViewController. swift
/// JieTableViewController. swift // JieTableView /// Created by jiezhang on 14-10-5. // Copyright (c) jiezhang. all rights reserved. // import UIKitclass JieTableViewController: UITableViewController {var listVideos: NSMutableArray! Override func viewDidLoad () {super. viewDidLoad () var bundle = NSBundle. mainBundle () let plistPath: String! = Bundle. pathForResource ("videos", ofType: "plist") listVideos = NSMutableArray (contentsOfFile: plistPath) // Uncomment the following line to preserve selection between presentations/self. clearsSelectionOnViewWillAppear = false // Uncomment the following line to display an Edit button in the navigation bar for this view controller. // In the following section, edit the settings on the left and add item self on the right. navigationItem. leftBarButtonItem = Self. editButtonItem () let addButton = UIBarButtonItem (barButtonSystemItem: UIBarButtonSystemItem. add, target: self, action: "insertNewObject:") self. navigationItem. rightBarButtonItem = addButton} func insertNewObject (sender: AnyObject) {var item: NSDictionary = NSDictionary (objects: ["http://c.hiphotos.baidu.com/video/pic/item/f703738da977391224eade15fb198618377ae2f2.jpg", "add data", NSDate. date (). Description], forKeys: ["video_img", "video_title", "video_subTitle"]) listVideos. insertObject (item, atIndex: 0) let indexPath = NSIndexPath (forRow: 0, inSection: 0) self. tableView. insertRowsAtIndexPaths ([indexPath], withRowAnimation :. automatic)} override func didReceiveMemoryWarning () {super. didReceiveMemoryWarning () // Dispose of any resources that can be recreated .} // MARK:-Table view dat A source // return the number of sections override func numberOfSectionsInTableView (tableView: UITableView)-> Int {// # warning Potentially incomplete method implementation. // Return the number of sections. return 1} // return the number of rows in a section override func tableView (tableView: UITableView, numberOfRowsInSection section: Int)-> Int {// # warning Incomplete method implementation. // Return the number of rows in the section. return l IstVideos. count} // provides data for table View cells. This method is required by override func tableView (tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)-> UITableViewCell {let cellIdentifier: string = "videoItem" let cell = tableView. dequeueReusableCellWithIdentifier (cellIdentifier, forIndexPath: indexPath) as JieTableViewCell var row = indexPath. row var rowDict: NSDictionary = listVideos. objectAtIndex (row) As NSDictionary let url: String = rowDict. objectForKey ("video_img") as String let dataImg: NSData = NSData (contentsOfURL: NSURL (string: url) cell. jieVideoImg. image = UIImage (data: dataImg) cell. jieVideoTitle. text = rowDict. objectForKey ("video_title")? String cell. JieVideoSubTitle. text = rowDict. objectForKey ("video_subTitle")? String return cell} override func tableView (tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {}// supports cell editing override func tableView (tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {// Return NO if you do not want the specified item to be editable. return true} // Override to support editing the table view. override func tableView (tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {if editingStyle =. delete {// Delete the row from the data source listVideos. removeObjectAtIndex (indexPath. row) tableView. deleteRowsAtIndexPaths ([indexPath], withRowAnimation :. fade)} else if editingStyle =. insert {// Create a new instance of the appropriate class, insert I T into the array, and add a new row to the table view} // Override to support rearranging the table view. override func tableView (tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {if fromIndexPath! = ToIndexPath {var object: AnyObject = listVideos. objectAtIndex (fromIndexPath. row) listVideos. removeObjectAtIndex (fromIndexPath. row) if toIndexPath. row> self. listVideos. count {self. listVideos. addObject (object)} else {self. listVideos. insertObject (object, atIndex: toIndexPath. row) }}// Override to support conditional rearranging of the table view. // In the editing status, you can drag to set the item position override func tableView (tab LeView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath)-> Bool {// Return NO if you do not want the item to be re-orderable. return true} // MARK:-Navigation // pass the override func prepareForSegue (segue: UIStoryboardSegue, sender: AnyObject!) to the new interface !) {If segue. identifier = "showDetail" {if let indexPath = self. tableView. indexPathForSelectedRow () {let object: NSDictionary = listVideos [indexPath. row] as NSDictionary (segue. destinationViewController as JieDetailViewController ). detailItem = object }}}}
JieTableViewCell. swift
/// JieTableViewCell. swift // JieTableView /// Created by jiezhang on 14-10-5. // Copyright (c) jiezhang. all rights reserved. // import UIKitclass JieTableViewCell: UITableViewCell {@ IBOutlet weak var JieVideoImg: UIImageView! @ IBOutlet weak var JieVideoTitle: UILabel! @ IBOutlet weak var JieVideoSubTitle: UILabel! Override func awakeFromNib () {super. awakeFromNib () // Initialization code} override func setSelected (selected: Bool, animated: Bool) {super. setSelected (selected, animated: animated )}}JieDetailViewController. swift
/// JieDetailViewController. swift // JieTableView /// Created by jiezhang on 14-10-5. // Copyright (c) jiezhang. all rights reserved. // import UIKitclass JieDetailViewController: UIViewController {@ IBOutlet var big_video_img: UIImageView! // Accept the passed value var detailItem: NSDictionary? Func configureView () {if let detail: NSDictionary = self. detailItem {self. title = detail. objectForKey ("video_title")? String let url: String = detail. objectForKey ("video_img") as String let dataImg: NSData = NSData (contentsOfURL: NSURL (string: url) self. big_video_img.image = UIImage (data: dataImg)} override func viewDidLoad () {super. viewDidLoad () configureView ()} override func didReceiveMemoryWarning () {super. didReceiveMemoryWarning () // Dispose of any resources that can be recreated .} // MARK:-Navig Ation override func prepareForSegue (segue: UIStoryboardSegue, sender: AnyObject !) {}}
Source Code address: https://github.com/jwzhangjie/JieTableView
[Core Data for ios development [3] How to Use NSFetchedResultsController to integrate TableView operation answers
Have you forgotten what you said before? Let's review it. In the first tutorial: "iOS Tutorial: core Data persistent storage basic tutorial describes how to create a Core Data Model and test method for an iOS program, we also connect the data model to a table view as a data source. In the second tutorial: iOS Tutorial: how to use Core Data-pre-load and import Data we talked about how to parse Data files of different formats to a general-purpose SQlite database of Core Data, and how to port this database to our iOS project, so that our application has some initial data. You can download the source code of the second part from here. Why NSFetchedResultsController? So far, we are just like using SQLite3, because Core Data is essentially operating the SQLite database, but we write less code than using SQLite directly, it is easier to use various database functions. However, we still have a very useful Core Data feature, which can greatly improve the performance of our program. It is: NSFetchedResultsController. In our example program, we load all the data into the view. This may be acceptable for our application, however, if an application has a large amount of data, the loading speed will become very slow and will also affect the user experience. Ideally, we only load the data that the user is browsing. Fortunately, Apple has provided an official method to do this, that is, NSFetchedResultsController. Therefore, open FBCDMasterViewController first. h. Add failedBankInfos, The NSArray array lightning, and add a pair to replace it: @ interface FBCDMasterViewController: UITableViewController @ property (nonatomic, strong) NSManagedObjectContext * managedObjectContext; @ property (nonatomic, retain) NSFetchedResultsController * fetchedResultsController; @ end in FBCDMasterViewController. in the synthesize section of m, delete the previous failedBankInfos synthesiz E Declaration, and add: @ synthesize fetchedResultsController = _ fetchedResultsController; another cool feature of NSFetchedResultsController Is That You Can redeclare it as nil in ViewDidUnload. This means that this method has an automatic memory management mechanism, that is, when the content is not in the screen, the occupied memory will be automatically cleared. All you need to do is declare it as null in ViewDidUnload. -(Void) viewDidUnload {self. fetchedResultsController = nil;}. Now we have an interesting part. Let's create a controller for the obtained data. First, we declare an attribute so that it can check whether the data exists with the running of the program and create it if it does not exist. Add the following code to the file header:-(NSFetchedResultsController *) fetchedResultsController {if (_ fetc ...... the remaining full text>
Ios develops a demo to display the view and tableview in viewcontroller normally.
Delegate, data source. Have you implemented this? "Controls on tableview"? Then you need to customize the cell, instead of directly dragging it to tableview. Go to code4app to get a demo.
Easy to ask, difficult to answer, and easy to answer!