微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

[IOSS]UITableView自定义cell

[IOSS]UITableView自定义cell

DEMO:http://download.csdn.net/detail/u012881779/9227615

应用入口(AppDelegate.swift)

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder,UIApplicationDelegate {

    var window: UIWindow?

    func application(application: UIApplication,didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        window?.backgroundColor = UIColor.whiteColor()
        
        let viewController = DMViewController()
        let nav = UINavigationController(rootViewController: viewController)
        nav.navigationBarHidden = true
        window?.rootViewController = nav
        
        window?.makeKeyAndVisible()
                
        return true
    }
}

列表控制器(DMViewController.swift)
import UIKit

class DMViewController: UIViewController,UITableViewDelegate,UITableViewDataSource{

    @IBOutlet weak var tableView: UITableView!
    var dataArr = NSMutableArray()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.delegate = self
        tableView.dataSource = self

        //数据模拟
        for(var i = 0 ; i < 20 ; i++ ){
            let dataDict = NSMutableDictionary()
            //整型->字符串
            var title = String(i)
            title.appendContentsOf("abc")

            //浮点->字符串
            let double = 20.12
            let doubleString = Nsstring(format: "%f",double)
            
            dataDict.setobject(title,forKey: "title")
            dataDict.setobject(doubleString,forKey: "double")
            dataDict.setobject(String(i),forKey: "id")
            
            dataArr.addobject(dataDict)
        }

    }

    
    //组数
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    
    //每组cell数
    func tableView(tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
        return dataArr.count
    }
    
    //赋值
    func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        /*认Cell
        let cell:UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle,reuseIdentifier:"cell");
        cell.textLabel!.text = "test1"
        */
       
        //自定义cell
        let cellIdentifier = "DMTableViewCell"
        self.tableView!.registerNib(UINib(nibName: "DMTableViewCell",bundle:nil),forCellReuseIdentifier: cellIdentifier)
        
        let cell : DMTableViewCell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier,forIndexPath: indexPath) as! DMTableViewCell
        
        if(dataArr.count > indexPath.row){
            cell.assignmentFromDictionary(dataArr.objectAtIndex(indexPath.row) as! NSDictionary)
        }
       
        return cell
    }
    
    //cell高度
    func tableView(tableView: UITableView,heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 60.0
    }
    
    //选中cell
    func tableView(tableView: UITableView,didSelectRowAtIndexPath indexPath: NSIndexPath) {
        print("选中某个cell");
    }
    
    /**/
    //能否编辑
    func tableView(tableView: UITableView,canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        return true
    }
    
    //cell编辑模式-这里选择显示Delete
    func tableView(tableView: UITableView,editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
        return UITableViewCellEditingStyle.Delete
    }
    
    //更改“Delete”为“删除”
    func tableView(tableView: UITableView,titleForDeleteConfirmationButtonForRowAtIndexPath indexPath: NSIndexPath) -> String? {
        return "删除"
    }
    
    //对选中的cell根据editingStyle进行操作
    func tableView(tableView: UITableView,commitEditingStyle editingStyle: UITableViewCellEditingStyle,forRowAtIndexPath indexPath: NSIndexPath) {
        if(editingStyle == UITableViewCellEditingStyle.Delete){
            //对数据进行操作
            dataArr.removeObjectAtIndex(indexPath.row)
            
            tableView.reloadData()
        }
    }
}
自定义cell(DMTableViewCell.swift)
import UIKit

class DMTableViewCell: UITableViewCell {

    @IBOutlet weak var leftImagview: UIImageView!
    
    @IBOutlet weak var idLab: UILabel!
    
    @IBOutlet weak var titleLab: UILabel!
    
    var dataDict = NSDictionary()
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(selected: Bool,animated: Bool) {
        super.setSelected(selected,animated: animated)

        // Configure the view for the selected state
    }
    
    //赋值
    func assignmentFromDictionary(valueDict : NSDictionary){
        dataDict = valueDict        
        idLab.text = valueDict.valueForKey("id") as? String
        titleLab.text = valueDict.objectForKey("title") as? String
        
    }
}
示意图:

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐