2019年3月2号

一、基本使用

        // 1.创建表格
        let tv = UITableView(frame: view.bounds, style: .plain)
        // 2.添加到视图
        view.addSubview(tv)
        // 3.注册可重用cell
        tv.register(UITableViewCell.self, forCellReuseIdentifier: "cellID")
        // 4.设置数据源
        tv.dataSource = self

二、解决IOS UITableView 滑不到最底部,总是被TabBar遮挡

在4.7或者以下的设备上,出现 UITableView 滑不到最底部,总是滑下去回弹到如图,  一直以为是适配和foot的高度问题.
解决办法:

1.将UITableView的高度减去底部TabBar的高度

       /// 屏幕长和宽
		let KWIDTH = UIScreen.main.bounds.size.width
		let KHEIGHT = UIScreen.main.bounds.size.height
		var bottomOffset:CGFloat = 0
        if let tb = tabBarController {
            bottomOffset = tb.tabBar.bounds.height
        }
    	var tableView = UITableView(frame: CGRect(x: 0, y: 0, width: KWIDTH, height: KHEIGHT-bottomOffset), style: .plain)

2.改变UITableView的滑动视图范围

      tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: bottomOffset, right: 0)

参考作者:chengs
链接:https://www.jianshu.com/p/c76bc6c935e7
来源:简书

在IOS7以后 ViewController 开始使用全屏布局的,而且是默认的行为通常涉及到布局
就离不开这个属性 edgesForExtendedLayout,它是一个类型为UIExtendedEdge的属性,
指定边缘要延伸的方向,它的默认值很自然地是UIRectEdgeAll,四周边缘均延伸,就是说,
如果即使视图中上有navigationBar,下有tabBar,那么视图仍会延伸覆盖到四周的区域。
因为一般为了不让tableView 不延伸到 navigationBar 下面, 属性设置为 UIRectEdgeNone
UIRectEdgeAll – default
UIRectEdgeNone
这时会发现导航栏变灰了,处理如下就OK了
self.navigationController.navigationBar.translucent = NO;

那 automaticallyAdjustsScrollViewInsets 呢?
当 automaticallyAdjustsScrollViewInsets为 NO 时,tableview 是从屏幕的最上边开始,也就是被
导航栏 & 状态栏覆盖
当 automaticallyAdjustsScrollViewInsets为 YES 时,也是默认行为,表现就比较正常了,和
edgesForExtendedLayout = UIRectEdgeNone有啥区别? 不注意可能很难觉察
设计师可能一眼就看穿。。。 automaticallyAdjustsScrollViewInsets 为YES 时,
tableView 上下滑动时,是可以穿过导航栏&状态栏的,在他们下面有淡淡的浅浅红色
extendedLayoutIncludesOpaqueBars
首先看下官方解释,默认 NO, 但是Bar 的默认属性是 透明的。。。也就是说只有在不透明下才有用
但是,测试结果很软肋,基本区别不大。。。但是对于解决一些Bug 是还是起作用的,比如说SearchBar的

作者:Smallwolf_JS
链接:https://www.jianshu.com/p/879fe48b0eb7
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

https://www.jianshu.com/p/9bef7ccccd88
附加样式CellStyle
UITableViewCell.CellStyle:
UITableViewCell.CellStyle.default
UITableViewCell.CellStyle.value1
UITableViewCell.CellStyle.value2
UITableViewCell.CellStyle.subtitle

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    var cell: UITableViewCell
   
    switch(indexPath.row)
    {
    case 1:
        cell = UITableViewCell(style: .Value1, reuseIdentifier: "Value1")
    case 2:
        cell = UITableViewCell(style: .Value2, reuseIdentifier: "Value2")
    case 3:
        cell = UITableViewCell(style: .Subtitle, reuseIdentifier: "Subtitle")
    default:
        cell = UITableViewCell(style: .Default, reuseIdentifier: "Default")
    }
   
    cell.textLabel?.text = "Hello"
    cell.detailTextLabel?.text = "World"
   
    return cell
}

AccessoryType附加属性
UITableViewCell.AccessoryType.checkmark
UITableViewCell.AccessoryType.detailButton
UITableViewCell.AccessoryType.detailDisclosureButton
UITableViewCell.AccessoryType.disclosureIndicator
UITableViewCell.AccessoryType.none

switch(indexPath.row)
{
case 1:
    cell = UITableViewCell(style: .Value1, reuseIdentifier: "Value1")
    cell.accessoryType = .DisclosureIndicator
case 2:
    cell = UITableViewCell(style: .Value2, reuseIdentifier: "Value2")
    cell.accessoryType = .DetailDisclosureButton
case 3:
    cell = UITableViewCell(style: .Subtitle, reuseIdentifier: "Subtitle")
    cell.accessoryType = .Checkmark
case 4:
    cell = UITableViewCell(style: .Subtitle, reuseIdentifier: "Subtitle2")
    cell.textLabel?.text = "Hello"
    cell.accessoryType = .DetailButton
    return cell
default:
    cell = UITableViewCell(style: .Default, reuseIdentifier: "Default")
    cell.accessoryType = .None
}

UITableViewCell().accessoryView:附加视图,如果提供附加视图,附加类型将被忽略。

cell.textLabel?.text = "Hello"
cell.detailTextLabel?.text = "World"
cell.accessoryView = UISwitch()

imageView属性,图像视图(左侧的图标)

cell.textLabel?.text = "Hello"
cell.detailTextLabel?.text = "World"
cell.accessoryView = UISwitch()
cell.imageView?.image = UIImage(named: "image")

总结。这是一个漫长的章节, 回顾一下我们看到的:
(1)4种不同的cell附加 样式,文字排列方式不同
(2)5个不同的cell附加属性
(3)在右边一个可定制的视图
(4)在左侧一个可自定义的图像视图

自定义UITableViewCell:
xib
http://www.hangge.com/blog/cache/detail_1591.html
http://www.hangge.com/blog/cache/detail_2194.html
http://www.hangge.com/blog/cache/detail_1499.html
代码

有时我们想让单元格与两侧保持有一定距离,下面通过样例进行演示如何实现。

1,样例代码
(1)关键点在于首先自定义一个单元格类(MyTableViewCell),并重写它的 frame 属性方法。

//自定义单元格
class MyTableViewCell: UITableViewCell {
    override var frame: CGRect {
        get {
            return super.frame
        }
        set {
            var frame = newValue
            frame.origin.x += 15
            frame.size.width -= 2 * 15
            super.frame = frame
        }
    }
}

原文出自:www.hangge.com 转载请保留原文链接:http://www.hangge.com/blog/cache/detail_2194.html

实现tableView中section分组圆角效果1(没有分区头、尾的情况)
(1)tableView 中每个 section 都带有圆角,无论该分区内是只有一个单元格,还是有多个单元格。
(2) 同时为了让显示效果更好,在每个单元格左右两侧还设置了边距,使其与边框保持一定的距离。
1)首先自定义一个单元格类(MyTableViewCell),并重写它的 frame 属性方法,实现边距效果。

import UIKit
//自定义单元格
class MyTableViewCell: UITableViewCell {
    override var frame: CGRect {
        get {
            return super.frame
        }
        set {
            var frame = newValue
            frame.origin.x += 15
            frame.size.width -= 2 * 15
            super.frame = frame
        }
    }
}


(2)主视图控制器代码如下,要实现圆角效果的关键在于 cellForRowAt 方法,根据不同情况对单元格设置不同的遮罩。

import UIKit
 
class ViewController: UIViewController , UITableViewDelegate, UITableViewDataSource{
     
    var tableView:UITableView?
     
    var allnames:Dictionary<Int, [String]>?
     
    override func loadView() {
        super.loadView()
    }
     
    override func viewDidLoad() {
        super.viewDidLoad()
         
        //初始化数据
        self.allnames =  [
            0:[String]([
                "UILabel 标签",
                "UITextField 文本框",
                "UIButton 按钮"]),
            1:[String]([
                "UIDatePiker 日期选择器",
                "UIToolbar 工具条"]),
            2:[String]([
                "UIProgressView 进度条"])
        ]
         
        //创建表视图
        self.tableView = UITableView(frame:self.view.frame, style:.grouped)
        self.tableView!.delegate = self
        self.tableView!.dataSource = self
        //创建一个重用的单元格
        self.tableView!.register(MyTableViewCell.self, forCellReuseIdentifier: "cell")
        self.view.addSubview(self.tableView!)
    }
     
    //返回分区
    func numberOfSections(in tableView: UITableView) -> Int {
        return self.allnames!.count
    }
     
    //返回表格行数(也就是返回控件数)
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let data = self.allnames?[section]
        return data!.count
    }
     
    //创建各单元显示内容(创建参数indexPath指定的单元)
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)
        -> UITableViewCell {
            //获取单元格
            let cell = self.tableView?.dequeueReusableCell(withIdentifier: "cell")
                as! MyTableViewCell
            cell.accessoryType = .disclosureIndicator
             
            //设置单元格内容
            let secno = indexPath.section
            var data = self.allnames?[secno]
            cell.textLabel?.text = data![indexPath.row]
         
            //圆角半径
            let cornerRadius:CGFloat = 15.0
             
            //下面为设置圆角操作(通过遮罩实现)
            let sectionCount = tableView.numberOfRows(inSection: indexPath.section)
            let shapeLayer = CAShapeLayer()
            cell.layer.mask = nil
            //当前分区有多行数据时
            if sectionCount > 1 {
                switch indexPath.row {
                //如果是第一行,左上、右上角为圆角
                case 0:
                    var bounds = cell.bounds
                    bounds.origin.y += 1.0  //这样每一组首行顶部分割线不显示
                    let bezierPath = UIBezierPath(roundedRect: bounds,
                          byRoundingCorners: [.topLeft,.topRight],
                          cornerRadii: CGSize(width: cornerRadius,height: cornerRadius))
                    shapeLayer.path = bezierPath.cgPath
                    cell.layer.mask = shapeLayer
                //如果是最后一行,左下、右下角为圆角
                case sectionCount - 1:
                    var bounds = cell.bounds
                    bounds.size.height -= 1.0  //这样每一组尾行底部分割线不显示
                    let bezierPath = UIBezierPath(roundedRect: bounds,
                          byRoundingCorners: [.bottomLeft,.bottomRight],
                          cornerRadii: CGSize(width: cornerRadius,height: cornerRadius))
                    shapeLayer.path = bezierPath.cgPath
                    cell.layer.mask = shapeLayer
                default:
                    break
                }
            }
            //当前分区只有一行行数据时
            else {
                //四个角都为圆角(同样设置偏移隐藏首、尾分隔线)
                let bezierPath = UIBezierPath(roundedRect:
                    cell.bounds.insetBy(dx: 0.0, dy: 2.0),
                    cornerRadius: cornerRadius)
                shapeLayer.path = bezierPath.cgPath
                cell.layer.mask = shapeLayer
            }
 
            return cell
    }
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值