[IOSS]UITableView分组
来源:互联网 发布:双手简谱制作软件 编辑:程序博客网 时间:2024/06/09 14:12
[IOSS]UITableView分组
DEMO:http://download.csdn.net/detail/u012881779/9233421
应用入口(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() var selectSection = NSInteger() override func viewDidLoad() { super.viewDidLoad() tableView.delegate = self tableView.dataSource = self tableView.separatorStyle = UITableViewCellSeparatorStyle.None selectSection = -1 //数据模拟 for(var i = 0 ; i < 10 ; i++ ){ let dataDict = NSMutableDictionary() //整型->字符串 var title = String(i) title.appendContentsOf("_section") //浮点->字符串 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") let tempMarr = NSMutableArray() for(var j = 0 ; j < 5 ; j++){ let sectionDict = NSMutableDictionary() var titlej = String(j) titlej.appendContentsOf("_row") let doublej = 41.32 let doubleStringj = NSString(format: "%f", doublej) sectionDict.setObject(titlej, forKey: "title") sectionDict.setObject(doubleStringj, forKey: "double") sectionDict.setObject(String(j), forKey: "id") tempMarr.addObject(sectionDict) } dataDict.setObject(tempMarr, forKey: "section") dataArr.addObject(dataDict) } } //组数 func numberOfSectionsInTableView(tableView: UITableView) -> Int { return dataArr.count } //每组cell数 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if(selectSection == -1){ return 1 }else{ if(selectSection == section){ return dataArr.objectAtIndex(selectSection).objectForKey("section")!.count + 1 }else{ return 1 } } } //赋值 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 cell.selectionStyle = UITableViewCellSelectionStyle.None if(selectSection == indexPath.section){ let selectDict = dataArr.objectAtIndex(indexPath.section) let sectionArr = selectDict.objectForKey("section") if(indexPath.row == 0){ cell.assignmentFromDictionary(dataArr.objectAtIndex(indexPath.section) as! NSDictionary ,andMark: 0) }else{ cell.assignmentFromDictionary(sectionArr!.objectAtIndex(indexPath.row-1) as! NSDictionary ,andMark: 1) } }else{ cell.assignmentFromDictionary(dataArr.objectAtIndex(indexPath.section) as! NSDictionary , andMark: 0) } return cell } //cell高度 func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { return 60.0 } //选中cell func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { if(selectSection == indexPath.section && indexPath.row == 0){ selectSection = -1 }else{ selectSection = indexPath.section } 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() @IBOutlet weak var sectionView: UIView! @IBOutlet weak var rowView: UIView! @IBOutlet weak var rowImgview: UIImageView! @IBOutlet weak var rowIdLab: UILabel! @IBOutlet weak var rowTitleLab: UILabel! override func awakeFromNib() { super.awakeFromNib() } override func setSelected(selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) } //赋值 func assignmentFromDictionary(valueDict : NSDictionary, andMark:NSInteger){ dataDict = valueDict if(andMark == 1){ sectionView.hidden = true rowView.hidden = false rowIdLab.text = valueDict.valueForKey("id") as? String rowTitleLab.text = valueDict.objectForKey("title") as? String }else{ sectionView.hidden = false rowView.hidden = true idLab.text = valueDict.valueForKey("id") as? String titleLab.text = valueDict.objectForKey("title") as? String } }}
示意图:
0 0
- [IOSS]UITableView分组
- [IOSS]UITableView自定义cell
- UITableView---分组
- UITableView的简单分组
- UITableView之分组案例
- ios UItableView,UITableViewHeaderFooterView分组
- UITableview 增加分组标题喝脚标
- UITableView之一分组简单使用
- [IOSS]左右侧边栏
- [IOSS]Swift数据类型转换
- iOSS系统分享
- IOS UITableView分组与索引分区实例
- iOS UItableView通讯录(分组显示)
- IOS UITableView分组与索引分区实例
- UITableView——实现汽车分组展示
- 仿QQ分组列表(UITableView)
- UITableView的分组示例及常用方法
- UICollectionReusableView的用法,类似UITableView的分组
- 算法与追MM
- Linux常用命令集锦
- Java 语言中 Enum 类型的使用介绍
- HDOJ 5532 Almost Sorted Array (正反LIS判断顺序)
- 可变参数列表(2)
- [IOSS]UITableView分组
- iOS中math.h常用函数说明
- Is it possible to change Agile Web Client login from port 7001 to the default port 80 in Weblogic?
- debian squeeze安装ibus
- [UVA 10891] Game of Sum
- Android:onNewIntent()触发机制及注意事项
- 2015前端框架何去何从
- 数据库连接
- 关于从activity跳转到fragment里面的问题