前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >iOS UITableView的使用

iOS UITableView的使用

作者头像
码客说
发布2019-10-22 14:41:59
8520
发布2019-10-22 14:41:59
举报
文章被收录于专栏:码客

代理

添加代理 UITableViewDelegate,UITableViewDataSource

数据

代码语言:javascript
复制
var tableData = [
        ["image":"lm_user","text":"账号"],
        ["image":"lm_photo","text":"相册"],
        ["image":"lm_star","text":"收藏"],
        ["image":"lm_file","text":"文件"],
        ["image":"lm_setting","text":"设置"],
        ["image":"lm_SIP","text":"SIP"]
    ]

Cell类

代码语言:javascript
复制
class LeftMenuCell: UITableViewCell {

    @IBOutlet weak var leftImageView: UIImageView!
    @IBOutlet weak var rightLabel: UILabel!
    
    override func awakeFromNib() {
        super.awakeFromNib()
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }
    
    func setItemData(itemData:[String:String])  {
        leftImageView.image = UIImage.init(named: itemData["image"]!);
        rightLabel.text = itemData["text"];
    }
    
}

初始化

初始化及代理方法

代码语言:javascript
复制
func initTableView(){
    //设置分割线样式
    self.tableView.separatorStyle = .none;
    self.tableView.register(UINib.init(nibName: "LeftMenuCell", bundle: nil), forCellReuseIdentifier: "LeftMenuCell");
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
}
    
func numberOfSections(in tableView: UITableView) -> Int {
    return 1;
}
    
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.tableData.count;
}
    
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 0.1;
}
    
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return 0.1;
}
    
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    return UIView();
}
    
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    return UIView();
}
    
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 60;
}
    
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let itemData  = self.tableData[indexPath.row];
    let  cell = tableView.dequeueReusableCell(withIdentifier: "LeftMenuCell", for: indexPath) as! LeftMenuCell;
    cell.setItemData(itemData: itemData);
    return cell;
}
    
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let itemData = self.tableData[indexPath.row];
    ZJFunc.unselectCell(tableView: tableView);
}

Cell自动高度

参见文章:iOS 设置tableViewCell的高度

代码中使用到的工具类

代码语言:javascript
复制
import UIKit
class ZJFunc{
    
    ///延迟执行方法
    static func delay(delay:Double, closure:@escaping ()->()) {
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + delay) {
            closure();
        }
    }
    
    ///异步调用方法
    static func async(block:@escaping ()->(),callBack:@escaping()->()){
        DispatchQueue(label: NSDate().description).async {
            block();
            DispatchQueue.main.async {
                callBack();
            }
        }
    }
    
    ///取消选中的tablecell
    static func unselectCell(tableView: UITableView)->Void{
        delay(delay: 0.1) {
            if(tableView.indexPathForSelectedRow != nil){
                tableView.deselectRow(at: tableView.indexPathForSelectedRow!, animated: true);
            }
        }
    }
    
    ///打电话
    static func phone(phone:String,viewController:UIViewController)->Void{
        let str = "tel:\(phone)";
        let callWebView = UIWebView();
        viewController.view.addSubview(callWebView);
        callWebView.loadRequest(URLRequest(url:URL(string:str)!))
    }
    
    ///发短信
    static func msg(phone:String)->Void{
        let str = "sms:\(phone)";
        UIApplication.shared.openURL(URL(string:str)!);
    }
    ///获取随机正整数
    static func randomNum(num:Int)->Int{
        let randomNum = Int(arc4random_uniform(UInt32(num)));
        return randomNum;
    }
    
    ///距离scrollView底部的距离
    static func scrollViewSpaceToButtom(scrollView: UIScrollView)->CGFloat{
        let offset = scrollView.contentOffset;
        let bounds = scrollView.bounds;
        let size = scrollView.contentSize;
        let inset = scrollView.contentInset;
        let currentOffset = offset.y + bounds.size.height - inset.bottom;
        let maximumOffset = size.height;
        
        //当currentOffset与maximumOffset的值相等时,说明scrollview已经滑到底部了。也可以根据这两个值的差来让他做点其他的什么事情
        let space = maximumOffset-currentOffset;
        return space;
    }
    
    ///判断系统是否大于某版本号
    static func isVersion(version:String)-> Bool{
        switch UIDevice.current.systemVersion.compare(version, options: NSString.CompareOptions.numeric) {
        case .orderedSame, .orderedDescending:
            return true;
        case .orderedAscending:
            return false;
        }
    }
    
    ///Cell抖动动画
    static func animateView(view: UIView) {
        let animation = CAKeyframeAnimation()
        animation.keyPath = "position.x"
        animation.values =  [0, 20, -20, 10, 0]
        animation.keyTimes = [
            NSNumber(value:0),
            NSNumber(value:1.0 / 6.0),
            NSNumber(value:3 / 6.0),
            NSNumber(value:5.0 / 6.0),
            NSNumber(value:1)
        ]
        animation.duration = 0.35
        animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)
        animation.isAdditive = true
        view.layer.add(animation, forKey: "shake")
    }
    
    
    static func animateViewY(view: UIView) {
        let animation = CAKeyframeAnimation()
        animation.keyPath = "position.y"
        animation.values =  [0, 10, 0, 5, 0]
        animation.keyTimes = [
            NSNumber(value:0),
            NSNumber(value:1.0 / 6.0),
            NSNumber(value:3 / 6.0),
            NSNumber(value:5.0 / 6.0),
            NSNumber(value:1)
        ]
        animation.duration = 0.35
        animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
        animation.isAdditive = true
        view.layer.add(animation, forKey: "shake")
    }
    
    static func animateViewBgColor(view: UIView) {
        let animation = CABasicAnimation()
        animation.keyPath = "backgroundColor"
        animation.fromValue = view.layer.backgroundColor;
        animation.toValue = UIColor.groupTableViewBackground.cgColor;
        
        animation.duration = 0.33
        animation.fillMode = kCAFillModeBoth
        view.layer.add(animation, forKey: nil)
        
        let animation2 = CABasicAnimation()
        animation2.keyPath = "cornerRadius"
        animation2.fromValue = view.layer.cornerRadius;
        animation2.toValue = view.frame.width/8;
        animation2.duration = 0.33
        animation2.fillMode = kCAFillModeBoth
        view.layer.add(animation2, forKey: nil)
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-12-06,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 代理
  • 数据
  • Cell类
  • 初始化
  • Cell自动高度
  • 代码中使用到的工具类
相关产品与服务
短信
腾讯云短信(Short Message Service,SMS)可为广大企业级用户提供稳定可靠,安全合规的短信触达服务。用户可快速接入,调用 API / SDK 或者通过控制台即可发送,支持发送验证码、通知类短信和营销短信。国内验证短信秒级触达,99%到达率;国际/港澳台短信覆盖全球200+国家/地区,全球多服务站点,稳定可靠。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档