UItableView的代理详解

2017-12-14  本文已影响0人  三少爷_贱
 //返回表格视图应该显示的数据的段数
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return dataArray!.count
 }
//返回表格视图上每段应该显示的数据的行数
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArray!.objectAtIndex(section).count
}
    //返回某行上应该显示的单元格
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        //定义一个cell标示符,用以区分不同的cell
        let cellID : String = "cell"
        
        //从cell复用池中拿到可用的cell
        /*
         我们为什么要实现单元格的复用机制?
         单元格每一个cell的生成都是要init alloc的,所以当我们滑动表格试图的时候会生成很多cell,无异于浪费了大量的内存
         单元格的复用机制原理
         一开始的时候我们创建了桌面最多能显示的单元格数,cell
         当我们向下滚动表格试图的时候,单元格上部的内容会消失,下部的内容会出现,这个时候我们将上部分消失的单元格赋给下部分出现的单元格
         因此我们就做到了只生成了屏幕范围可显示的单元格个数,就实现滑动表格试图时,以后不会再init alloc单元格cell了,从而实现了节省内存的原理
         */
        var cell = tableView.dequeueReusableCellWithIdentifier(cellID) as UITableViewCell!
        //检测,拿到一个可用的cell
        if(cell == nil)
        {
            //创建新的cell
            /*
             UITableViewCellStyle.Default,    // 左侧显示textLabel(不显示detailTextLabel),imageView可选(显示在最左边)
             UITableViewCellStyle.Value1,        // 左侧显示textLabel、右侧显示detailTextLabel(默认蓝色),imageView可选(显示在最左边)
             UITableViewCellStyle.Value2,        // 左侧依次显示textLabel(默认蓝色)和detailTextLabel,imageView可选(显示在最左边)
             UITableViewCellStyle.Subtitle    // 左上方显示textLabel,左下方显示detailTextLabel(默认灰色),imageView可选(显示在最左边)
             */
            cell = UITableViewCell.init(style: UITableViewCellStyle.Subtitle, reuseIdentifier: cellID)
        }
        let array =  self.dataArray!.objectAtIndex(indexPath.section)
        let dic = array.objectAtIndex(indexPath.row)
    //cell上文本
        cell.textLabel?.textColor = UIColor.blackColor()
        cell.textLabel?.text = dic.objectForKey("title") as! String//cell上的子标题
        cell.detailTextLabel?.text = dic.objectForKey("detail") as! String
        //cell上图片
        cell.imageView?.image = UIImage(named: dic.objectForKey("image") as! String)
        
        return cell;
    }
    //设置分段的头标题
    func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        //如果是第0个分段
        if(section == 0){
            return "第 0 段段头"
        }
        //否则,(即第一个分段)
        return "第 1 段段头"
    }
    //设置分段脚标题
    func tableView(tableView: UITableView, titleForFooterInSection section: Int) -> String? {
        
        if(section == 0){
            
            return "第 0 段段尾"
        }
        return "第 1 段段尾"
    }
    //右边索引字节数(如果不实现 就不显示右侧索引)
func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]? {
//    
//        return self.sectionTitleArray
//    }
//
//    //点击右边的索引,调用该方法
//    func tableView(tableView: UITableView, sectionForSectionIndexTitle title: String, atIndex index: Int) -> Int {
//        
//        let key = self.sectionTitleArray?.objectAtIndex(index)
//        print("sectionForSectionIndexTitle key=%@",key);
//        if (key == UITableViewIndexSearch) {
//            
//            myTableView?.setContentOffset(CGPointZero, animated: false)
//            
//            return NSNotFound
//        }
//        
//        return index
//    }
    //让表格可以修改,滑动可以修改
    func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        
        return true
    }
    //允许移动
    func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        
        return true
    }
    
    //执行删除或者插入
    func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        
        //删除
        if (editingStyle == UITableViewCellEditingStyle.Delete) {
            
            let row = indexPath.row
            self.dataArray?.removeObjectAtIndex(row)
            let  array = [indexPath]
            tableView.deleteRowsAtIndexPaths(array, withRowAnimation: UITableViewRowAnimation.Automatic)
            
            /*
             UITableViewRowAnimationAutomatic
             UITableViewRowAnimationTop
             UITableViewRowAnimationBottom
             UITableViewRowAnimationLeft
             UITableViewRowAnimationRight
             UITableViewRowAnimationMiddle
             UITableViewRowAnimationFade
             UITableViewRowAnimationNone
             */
        }
            //插入
        else if (editingStyle == UITableViewCellEditingStyle.Insert){
            
            //我们实现的是在所选行的位置插入一行,因此直接使用了参数indexPath
            let insertIndexPaths = [indexPath]
            //同样,将数据加到list中,用的row
            //        [self.dataArray insertObject:@"新添加的行" atIndex:indexPath.];
            tableView.insertRowsAtIndexPaths(insertIndexPaths, withRowAnimation: UITableViewRowAnimation.Right)
        }

    }
    
    //开始移动row时执行
    func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {
     
        let fromRow = sourceIndexPath.row
        let toRow = destinationIndexPath.row
        
        let object = self.dataArray?.objectAtIndex(sourceIndexPath.section).objectAtIndex(fromRow)
        
        self.dataArray?.removeObjectAtIndex(fromRow)
//        self.dataArray?.insertObject(object, atIndex: toRow)
    }
    //移动row时执行
    func tableView(tableView: UITableView, targetIndexPathForMoveFromRowAtIndexPath sourceIndexPath: NSIndexPath, toProposedIndexPath proposedDestinationIndexPath: NSIndexPath) -> NSIndexPath {
        print("targetIndexPathForMoveFromRowAtIndexPath")
        //用于限制只在当前section下面才可以移动
        if(sourceIndexPath.section != proposedDestinationIndexPath.section){
            return sourceIndexPath
        }
        return proposedDestinationIndexPath
    }
    //打开编辑模式后,默认情况下每行左边会出现红的删除按钮
    func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
        
        return UITableViewCellEditingStyle.Delete
    }
    //返回每一行的行高
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        
        return 60;
    }
    //返回每一段段头的高度
    func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        
        return 40;
    }

    //返回每一段段尾的高度
    func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        
        return 40;
    }
    //自定义头标题的view
    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        let label = UILabel()
        label.backgroundColor = UIColor.grayColor()
        label.textAlignment = NSTextAlignment.Center;
        label.font = UIFont.systemFontOfSize(20)
        label.textColor = UIColor.blackColor()
        
        //如果分段为第0段
        if(section == 0){
            label.text = "第0段的断头"
        }
            //如果分段为第一段
        else{
            
            label.text = "第 1 段的断头"
        }
        //return的view会替换自带的头标题view
        //frame设置无效,位置是固定的,宽度固定和table一样,高度通过代理方法设置
        return label
    }
    //自定义尾标题的view
    func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        
        let label = UILabel()
        label.backgroundColor = UIColor.redColor()
        label.textAlignment = NSTextAlignment.Center
        label.font = UIFont.systemFontOfSize(20)
        label.textColor = UIColor.blackColor()
        
        //如果分段为第0段
        if(section == 0){
            label.text = "第0段的断尾";
        }
            //如果分段为第一段
        else{
            label.text = "第 1 段的断尾";
        }
        return label
    }
    //cell将要显示
    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
       
    }
    //cell已经显示
    func tableView(tableView: UITableView, didEndDisplayingCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        
    }
    
    //头View将要显示
    func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    }
    //头View已经显示
    func tableView(tableView: UITableView, didEndDisplayingHeaderView view: UIView, forSection section: Int) {
        
    }
    //尾View将要显示
    func tableView(tableView: UITableView, willDisplayFooterView view: UIView, forSection section: Int) {
        
    }
    //尾View已经显示
    func tableView(tableView: UITableView, didEndDisplayingFooterView view: UIView, forSection section: Int) {
        
        
    }
    //UITableViewCell的附件属性
    func tableView(tableView: UITableView, accessoryTypeForRowWithIndexPath indexPath: NSIndexPath) -> UITableViewCellAccessoryType {
        
        if (indexPath.section == 0 && indexPath.row == 3) {
            
            return UITableViewCellAccessoryType.Checkmark
        }
        
        return UITableViewCellAccessoryType.None
    }

    //点击了附加图标时执行
    func tableView(tableView: UITableView, accessoryButtonTappedForRowWithIndexPath indexPath: NSIndexPath) {
        
    }
    //是否将要点亮某一行
    func tableView(tableView: UITableView, shouldHighlightRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        
        return true
    }
    
    //是否已经点亮某一行
    func tableView(tableView: UITableView, didHighlightRowAtIndexPath indexPath: NSIndexPath) {
        
    }
    //不点量某一行
    func tableView(tableView: UITableView, didUnhighlightRowAtIndexPath indexPath: NSIndexPath) {
        
        
    }
    //将要选中某一行
    func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {
     
        return indexPath
    }
    //已经选中某一行
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        
        
    }
    //将要反选某一行
    func tableView(tableView: UITableView, willDeselectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {
        
        return indexPath
    }
    //已经反选了某一行
    func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
        
    }
    //返回删除
    func tableView(tableView: UITableView, titleForDeleteConfirmationButtonForRowAtIndexPath indexPath: NSIndexPath) -> String? {
        
        return "删除"
    }
    // 8.0后侧滑菜单的新接口,支持多个侧滑按钮。
    func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
        
        return [UITableViewRowAction.init(style: UITableViewRowActionStyle.Normal, title: "编辑", handler: { (UITableViewRowAction, indexPath) in
                
                
            }),UITableViewRowAction.init(style: UITableViewRowActionStyle.Default, title: "删除", handler: { (UITableViewRowAction, indexPath) in
                
                
            })]
    }
    //编辑模式下,让行缩进
    func tableView(tableView: UITableView, shouldIndentWhileEditingRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        
        return true
    }
    //行缩进
    func tableView(tableView: UITableView, indentationLevelForRowAtIndexPath indexPath: NSIndexPath) -> Int {
        
        return indexPath.row
        
    }
    //将要开始编辑某一行
    func tableView(tableView: UITableView, willBeginEditingRowAtIndexPath indexPath: NSIndexPath) {
        
        
    }
//结束编辑某一行
    func tableView(tableView: UITableView, didEndEditingRowAtIndexPath indexPath: NSIndexPath) {
    }
//允许Menu菜单
    func tableView(tableView: UITableView, shouldShowMenuForRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        return true
    }
    //每个cell都可以点击出现Menu菜单
    func tableView(tableView: UITableView, canPerformAction action: Selector, forRowAtIndexPath indexPath: NSIndexPath, withSender sender: AnyObject?) -> Bool {
        
        if (action == #selector(cut(_:))){
            
            
            return false
            
        }  else if(action == #selector(copy(_:))){
            
            
            return false
            
        }
        
        else if(action == #selector(paste(_:))){
            
            
            return false
            
        }
        
        else if(action == #selector(select(_:))){
            
            
            return false
            
        }
        
        else if(action == #selector(selectAll(_:))){
            
            
            return false
            
        }else{
            
            return super.canPerformUnwindSegueAction(action, fromViewController: self, withSender: sender)
            
        }

    }
    func tableView(tableView: UITableView, performAction action: Selector, forRowAtIndexPath indexPath: NSIndexPath, withSender sender: AnyObject?) {
        
        if (action == #selector(copy(_:))) {
            
            
        }
        
        if (action == #selector(cut(_:))) {
            
            
        }
        
        if (action == #selector(paste(_:))) {
            
            
        }

        
    }
    //焦点
    func tableView(tableView: UITableView, canFocusRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        
        return true
    }
    func tableView(tableView: UITableView, shouldUpdateFocusInContext context: UITableViewFocusUpdateContext) -> Bool {
        
        return true
    }
    func tableView(tableView: UITableView, didUpdateFocusInContext context: UITableViewFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {

    }
上一篇 下一篇

猜你喜欢

热点阅读