[IOSS]UITableView分组

[IOSS]UITableView分组

DEMO:http://download.csdn.net/detail/u012881779/9233421

应用入口(AppDelegate.swift)

import UIKit

@UIApplicationMain
class 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 UIKit

class 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 UIKit

class 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
        }
    }
}

示意图:


相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...