Swift - TableView

来源:互联网 发布:波什程序员 编辑:程序博客网 时间:2024/06/05 15:28

////  MyTV.swift//  Tableview////  Created by admin on 16/2/1.//  Copyright © 2016年 admin. All rights reserved.//import UIKitclass MyTV: UITableView,UITableViewDataSource ,UITableViewDelegate {       var data:NSDictionary!        required init(coder aDecoder: NSCoder) {        super.init(coder: aDecoder)!        self.dataSource = self   //********        self.delegate = self     //********        data  = NSDictionary(contentsOfURL: NSBundle.mainBundle().URLForResource("data", withExtension: "plist")!)    }        func numberOfSectionsInTableView(tableView: UITableView) -> Int {        return data.count    }        func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {        let cell = tableView.dequeueReusableCellWithIdentifier("cell")  //****************        let label = cell?.viewWithTag(1) as! UILabel<span style="white-space:pre"></span>//****************       label.text = data.allValues[indexPath.section].objectAtIndex(indexPath.row) as? String        return cell!    }    func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String?{        return data.allKeys[section] as? String    }    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {        return data.allValues[section].count    }    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){        print("\(data.allValues[indexPath.section].objectAtIndex(indexPath.row))    ")    }    /*    // Only override drawRect: if you perform custom drawing.    // An empty implementation adversely affects performance during animation.    override func drawRect(rect: CGRect) {        // Drawing code    }    */}


- dequeueReusableCellWithIdentifier:Returns a reusable table-view cell object located by its identifier.DeclarationSWIFTfunc dequeueReusableCellWithIdentifier(_ identifier: String) -> UITableViewCell?OBJECTIVE-C- (__kindofUITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifierParametersidentifierA string identifying the cell object to be reused. This parameter must not be nil.Return ValueA UITableViewCell object with the associated identifier or nil if no such object exists in the reusable-cell queue.DiscussionFor performance reasons, a table view’€™s data source should generally reuse UITableViewCell objects when it assigns cells to rows in its tableView:cellForRowAtIndexPath: method. A table view maintains a queue or list of UITableViewCell objects that the data source has marked for reuse. Call this method from your data source object when asked to provide a new cell for the table view. This method dequeues an existing cell if one is available or creates a new one using the class or nib file you previously registered. If no cell is available for reuse and you did not register a class or nib file, this method returns nil.If you registered a class for the specified identifier and a new cell must be created, this method initializes the cell by calling its initWithStyle:reuseIdentifier: method. For nib-based cells, this method loads the cell object from the provided nib file. If an existing cell was available for reuse, this method calls the cell’s prepareForReuse method instead.AvailabilityAvailable in iOS 2.0 and later.

- viewWithTag:Returns the view whose tag matches the specified value.DeclarationSWIFTfunc viewWithTag(_ tag: Int) -> UIView?OBJECTIVE-C- (__kindof UIView *)viewWithTag:(NSInteger)tagParameterstagThe tag value to search for.Return ValueThe view in the receiver’s hierarchy whose tag property matches the value in the tag parameter.DiscussionThis method searches the current view and all of its subviews for the specified view.AvailabilityAvailable in iOS 2.0 and later.




错误原因:类中未实现sections和rows的函数

解决办法:添加如下函数



----------------------------------------------------------



错误原因:rows和数组的个数不匹配

解决办法:func tableView(tableView:UITableView, numberOfRowsInSection section:Int) ->Int中返回数组的个数



==----------------------------------------------------------------



0 0