[IOSS]UItableVIEw自定义cell
DEMO:http://download.csdn.net/detail/u012881779/9227615
应用入口(AppDelegate.swift)
import UIKit@UIApplicationMainclass 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 UIKitclass 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 UIKitclass 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 }}示意图: 总结
以上是内存溢出为你收集整理的[IOSS]UITableView自定义cell全部内容,希望文章能够帮你解决[IOSS]UITableView自定义cell所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)