记得上一次使用Swift还是去年的10月份,大概10月下旬的时候,拿到手一份纯Swift写的项目,而且是用1.2的版本写的。可想而知,当时拿到代码时的心情,简直一万只草泥马奔腾而过。
在11月初,开始学习Swift2.0,其中控件的使用,基本上都差不多,只是代码语言的更换方式,熟悉OC的人,能很快切换到Swift。我在学习的途中,也遇到的很多的坑点,其中一个坑点就是tableViewCell的使用。时隔3,4个月,很多东西都忘记了,现在重新记录一下。
Swift创建控件的时候,需要在父类的后面加上”?”或者”!”。其中”?”表示判断解析、”!”表示强制解析。使用cell的时候,什么时候使用”?”,什么时候使用”!”,开始我是真的不清楚,后来学习了一下。在后面的代码中,会有讲解
现在不如正题:
首先创建tableView,这个应该都不陌生
var tableView : UITableView! (这里使用"!",强制解析,当然也可以使用"?",不过代价就是,要在实例化之后的的每一个tableView后面加上"!",想想就可怕,所以还是使用"!"吧)
self.tableView = UITableView.init(frame: self.view.bounds, style: UITableViewStyle.Grouped)
self.tableView.delegate = self
self.tableView.dataSource = self
self.view.addSubview(self.tableView)
//创建数据源
for i in 1...20 {
self.dataList.append("第\(i)行")
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.dataList.count
}
//使用纯代码方式创建cell
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellId = "cellId"
var cell = tableView.dequeueReusableCellWithIdentifier(cellId) as? MyOrderCell //注意:此处要加上as? MyOrderCell(cell的类名,使用"?")
if cell == nil
{
cell = MyOrderCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: cellId)
}
return cell! //注意:cell后面要加上"!"
}
//使用xib创建cell
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//注册cell
self.tableView.registerNib(UINib(nibName: "ManageAddressCell", bundle: nil), forCellReuseIdentifier: "ManageAddressCell")
let cell = tableView.dequeueReusableCellWithIdentifier("ManageAddressCell", forIndexPath: indexPath) as! ManageAddressCell //注意:此处用的是"!",没错,xib创建cell,此处要对cell的类进行强制解析
cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
cell.selectionStyle = UITableViewCellSelectionStyle.None
return cell //注意:此处不需要加"!"了,为什么呢?因为上面创建cell,已经对cell进行强制解析了,所以此处是不用写"!"的
}
// iOS 8推出的,在一个cell上面添加多个按钮,适配iOS7的项目,请自行编写cell上的按钮
func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
let action1 = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "删除", handler: { (action, Indexable) -> Void in
//同步本地数据
self.dataList.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Fade)
})
let action2 = UITableViewRowAction(style: UITableViewRowActionStyle.Normal, title: "关注", handler: { (action, Indexable) -> Void in
let alertVC = UIAlertController.init(title: nil, message: "已关注", preferredStyle: UIAlertControllerStyle.Alert)
self.presentViewController(alertVC, animated: true, completion: nil)
//延迟3秒执行(延迟执行不建议使用perform方式,此处为了方便才使用,原谅我的懒惰)
self.performSelector("dismissVC", withObject: nil, afterDelay: 3)
})
let action3 = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "button", handler: { (action, Indexable) -> Void in
print("这是一个按钮")
self.tableView.reloadData()
})
return [action1,action2,action3]
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if indexPath.row == 0 {
return 100
}
return 66
}
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 0.1
}
func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 0.1
}
// 移除弹出框
func dismissVC()
{
self.presentedViewController?.dismissViewControllerAnimated(false, completion: nil)
self.tableView.reloadData()
}
到这里,基本上的使用就这么多,可能会觉得,没什么坑点啊。其实这里面的最大的坑点,就是xib创建cell和纯代码创建cell中,as的时候,记得最开始的时候,真的弄了很久才能让代码跑起来…可能是我真的太坑…感觉Swift还会有很多坑点,路漫漫其修远兮 吾将上下而求索。