https://github.com/potato512/SYSwiftLearning
像手机中的联系人通讯录,在联系人列表的右侧有个字母索引,然后通过点击字母索引,就可以快速定位到以该字母为首字母的联系人分组。
效果图如下:
实现列表视图的索引功能,主要在于设置索引标题,以及实现索引代理方法。
具体代码如下所示:
// 定义变量var maintableVIEw:UItableVIEw!var mainArray:NSMutableArray!let sortArray = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
// MARK: - 数据func setLocalData(){ self.mainArray = NSMutableArray() for charTmp in self.sortArray { let array = NSMutableArray() let number = random() % 10 + 1 for index in 1...number { let text = String(format: "%@-%ld",arguments: [charTmp,index]) array.addobject(text) } let dict = NSMutableDictionary() dict.setobject(array,forKey: "sectionArray") dict.setobject(charTmp,forKey: "sectionTitle") self.mainArray.addobject(dict) }}
// MARK: - 视图func setUI(){ self.maintableVIEw = UItableVIEw(frame: self.vIEw.bounds,style: .Plain) self.vIEw.addSubvIEw(self.maintableVIEw) self.maintableVIEw.autoresizingMask = .FlexibleHeight self.maintableVIEw.backgroundcolor = UIcolor.clearcolor() self.maintableVIEw.delegate = self self.maintableVIEw.dataSource = self self.maintableVIEw.tableFooterVIEw = UIVIEw.init() // 索引 // 设置索引值颜色 self.maintableVIEw.sectionIndexcolor = UIcolor.bluecolor() // 设置选中时的索引背景颜色 self.maintableVIEw.sectionIndexTrackingBackgroundcolor = UIcolor.clearcolor(); // 设置索引的背景颜色 self.maintableVIEw.sectionIndexBackgroundcolor = UIcolor.clearcolor()}
// MARK: - UItableVIEwDelegate,UItableVIEwDataSourcefunc tableVIEw(tableVIEw: UItableVIEw,numberOfRowsInSection section: Int) -> Int { let dict:NSDictionary! = self.mainArray.objectAtIndex(section) as! NSDictionary let array:NSArray! = dict.objectForKey("sectionArray") as! NSArray return array.count} func tableVIEw(tableVIEw: UItableVIEw,cellForRowAtIndexPath indexPath: NSIndexPath) -> UItableVIEwCell { var cell:UItableVIEwCell! = tableVIEw.dequeueReusableCellWithIDentifIEr("UItableVIEwCell") if cell == nil { cell = UItableVIEwCell(style: .Default,reuseIDentifIEr: "UItableVIEwCell") } let dict:NSDictionary! = self.mainArray.objectAtIndex(indexPath.section) as! NSDictionary let array:NSArray! = dict.objectForKey("sectionArray") as! NSArray let text:String! = array.objectAtIndex(indexPath.row) as! String cell.textLabel!.text = text return cell} func tableVIEw(tableVIEw: UItableVIEw,dIDSelectRowAtIndexPath indexPath: NSIndexPath) { tableVIEw.deselectRowAtIndexPath(indexPath,animated: true)}
// MARK: 索引(使用索引功能时,必须是有分组的列表,即有section分组)// section分组func numberOfSectionsIntableVIEw(tableVIEw: UItableVIEw) -> Int { return self.mainArray.count} // section分组标题(通常与索引值数组相同)func tableVIEw(tableVIEw: UItableVIEw,TitleForheaderInSection section: Int) -> String? { let dict:NSDictionary! = self.mainArray.objectAtIndex(section) as! NSDictionary let text:String! = dict.objectForKey("sectionTitle") as! String return text} // 索引值数组func sectionIndexTitlesFortableVIEw(tableVIEw: UItableVIEw) -> [String]? { return self.sortArray} // 索引值与列表关联点击事件func tableVIEw(tableVIEw: UItableVIEw,sectionForSectionIndexTitle Title: String,atIndex index: Int) -> Int { return index}总结
以上是内存溢出为你收集整理的swift中UITableView的使用(索引功能)全部内容,希望文章能够帮你解决swift中UITableView的使用(索引功能)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)