这段时间比较忙,没太多的时间写博客,前段时间写了一些关于表格视图单选的文章,想着,一并把多选也做了,今天刚好有时间,去做这样一件事情。多选在我们的应用程序中也是常见的,比如消息的删除,群发联系人的选择,音乐的添加等等可能都会涉及到多选的需求,本文,我将模拟多选删除消息来讲讲多选的实现。
原理多选删除其实很简单,并不复杂,我的思路就是创建一个数组,当用户选中某个单元格的时候,取到单元格上对应的数据,把它存入数组中,如果用户取消选中,直接将数据从数组中移除。当用户点击删除时,直接遍历数组,将表格视图数据源数组里面的与存选择数据的数组中的数据相对应一一删除,再刷新表格视图即可。
实现界面搭建很简单,创建一个表格视图,添加导航栏,并在上面添加一个删除按钮,这里我就不细说了。
在 VIEwController
中,我们先声明几个属性,其中 selectedDatas
主要用于记录用户选择的数据。
var tableVIEw: UItableVIEw?var dataSource: [String]?var selectedDatas: [String]?@H_502_32@创建初始化方法,初始化属性,别忘了还需要在
VIEwDIDLoad()
中调用方法初始化方法。// MARK:Initialize methodsfunc initializeDatasource() { self.selectedDatas = [] self.dataSource = [] for index in 1...10 { index < 10 ? self.dataSource?.append("消息0\(index)") : self.dataSource?.append("消息\(index)") }}func initializeUserInterface() { self.Title = "消息" self.automaticallyAdjustsScrollVIEwInsets = false // Add delete navigation item self.navigationItem.leftbarbuttonItem = UIbarbuttonItem(Title: "删除",style: UIbarbuttonItemStyle.Plain,target: self,action: Selector("respondsTobarbuttonItem:")) // table vIEw self.tableVIEw = { let tableVIEw = UItableVIEw(frame: CGRectMake(0,64,CGRectGetWIDth(self.vIEw.bounds),CGRectGetHeight(self.vIEw.bounds)),style: UItableVIEwStyle.Plain) tableVIEw.dataSource = self tableVIEw.delegate = self tableVIEw.tableFooterVIEw = UIVIEw() return tableVIEw }() self.vIEw.addSubvIEw(self.tableVIEw!) }@H_502_32@此时,系统会报警告,提示你没有遵守协议,因为我们在初始化表格视图的时候为其设置了代理和数据源,遵守协议,并实现协议方法,配置数据源。
func numberOfSectionsIntableVIEw(tableVIEw: UItableVIEw) -> Int { return 1}func tableVIEw(tableVIEw: UItableVIEw,numberOfRowsInSection section: Int) -> Int { return self.dataSource!.count}func tableVIEw(tableVIEw: UItableVIEw,cellForRowAtIndexPath indexPath: NSIndexPath) -> UItableVIEwCell { var cell: CustomtableVIEwCell? = tableVIEw.dequeueReusableCellWithIDentifIEr("cellReuseIDentifIEr") as? CustomtableVIEwCell if cell == nil { cell = CustomtableVIEwCell(style: UItableVIEwCellStyle.Value1,reuseIDentifIEr: "cellReuseIDentifIEr") } cell!.selectionStyle = UItableVIEwCellSelectionStyle.None cell!.textLabel?.text = self.dataSource![indexPath.row] cell!.detailTextLabel?.text = "昨天 10-11" return cell!}@H_502_32@这里需要强调的是,表格视图的单元格是自定义的,在自定义单元格(
CustomtableVIEwCell
)中我只做了一个 *** 作,就是根据单元格选中状态来切换图片的显示。overrIDe func setSelected(selected: Bool,animated: Bool) { super.setSelected(selected,animated: animated) if selected { self.imageVIEw?.image = UIImage(named: "iconFont-selected") }else { self.imageVIEw?.image = UIImage(named: "iconFont-select") }}@H_502_32@现在运行程序,界面已经显示出来了,下面我们将开始处理多选删除的逻辑,当我们点击单元格的时候发现,只能单选啊?我要怎么去多选呢?此时我们需要在配置表格视图的地方设置
allowsMultipleSelection
属性,将其值设为YES
。tableVIEw.allowsMultipleSelection = true@H_502_32@接下来,实现代理方法
dIDSelectRowAtIndexPath:
与dIDdeselectRowAtIndexPath:
,这里我们将修改单元格选中与未选中状态下的颜色,只需根据参数indexPath
获取到单元格,修改backgroundcolor
属性,并且,我们还需要获取单元格上的数据,去 *** 作selectedDatas
数组,具体实现如下。func tableVIEw(tableVIEw: UItableVIEw,dIDSelectRowAtIndexPath indexPath: NSIndexPath) { let cell = tableVIEw.cellForRowAtIndexPath(indexPath) cell?.backgroundcolor = UIcolor.cyancolor() self.selectedDatas?.append((cell!.textLabel?.text)!)}func tableVIEw(tableVIEw: UItableVIEw,dIDdeselectRowAtIndexPath indexPath: NSIndexPath) { let cell = tableVIEw.cellForRowAtIndexPath(indexPath) cell?.backgroundcolor = UIcolor.whitecolor() let index = self.selectedDatas?.indexOf((cell?.textLabel?.text)!) self.selectedDatas?.removeAtIndex(index!)}@H_502_32@在
dIDdeselectRowAtIndexPath:
方法中,我是根据表格视图单元格上的数据获取下标,再从数组中删除元素的,可能有的人会问,不能像OC一样调用removeObject:
方法根据数据直接删除元素吗?并不能,因为Swift 提供的删除数组元素的方法中,大都是根据下标来删除数组元素的。接下来,我们需要执行删除逻辑了,在删除按钮触发的方法中,我们要做的第一件事情就是异常处理,如果
selectedDatas
数组为空或者该数组并未初始化,我们无需再做删除处理,d框提示即可。// Exception handlingif (self.selectedDatas == nil) || (self.selectedDatas?.isEmpty == true) { let alertController = UIAlertController(Title: "温馨提示",message: "请选择您要删除的数据!",preferredStyle: UIAlertControllerStyle.Alert) alertController.addAction(UIAlertAction(Title: "确定",style: UIAlertActionStyle.Default,handler: nil)) self.presentVIEwController(alertController,animated: true,completion: nil) return}@H_502_32@异常处理之后,我们需要遍历
selectedDatas
数组,然后根据该数组中的数据获取到该数据在数据源(dataSource
)里的下标,最后再根据该下标将数据从数据源中删除。for data in self.selectedDatas! { // Get index with data let index = self.dataSource!.indexOf(data) // Delete data with index self.dataSource?.removeAtIndex(index!)}@H_502_32@现在我们只需要刷新表格视图即可,当然,
selectedDatas
数组我们也需要清空,以备下一次用户多选删除使用。self.tableVIEw?.reloadData()self.selectedDatas?.removeAll()@H_502_32@为了提高用户体验,我们可以d框提示用户删除成功,直接在后面加上以下代码。
let alertController = UIAlertController(Title: "温馨提示",message: "数据删除成功!",preferredStyle: UIAlertControllerStyle.Alert)self.presentVIEwController(alertController,animated: true,completion: nil)dispatch_after(dispatch_time(disPATCH_TIME_Now,Int64(1.0 * Double(NSEC_PER_SEC))),dispatch_get_main_queue()) { () -> VoID in self.presentedVIEwController?.dismissVIEwControllerAnimated(true,completion: nil)}@H_502_32@代码中,我并未给d出框添加
action
,而是通过一个延迟调用,来让d出框自动消失,这样就避免了用户再次点击d出框按钮来隐藏,提升用户体验。OK,到了这一步,基本已经实现了,但是有一个小瑕疵,那就是当我删除了单元格的时候,界面上某些单元格会呈现选中状态的背景颜色,解决办法非常简单,我们只需要在配置单元格的协议方法中加上这一句话即可。
cell?.backgroundcolor = UIcolor.whitecolor()@H_502_32@ 总结以上是内存溢出为你收集整理的Swift:表格视图单元格多选全部内容,希望文章能够帮你解决Swift:表格视图单元格多选所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)