微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

swift中UITableView的使用常规使用

源码:https://github.com/potato512/SYSwiftLearning



// MARK: - 初始化tableview
    
func setUI()
{
        // 初始化tableView
        self.mainTableView = UITableView(frame:self.view.bounds,style:UITableViewStyle.Plain)
        self.view.addSubview(self.mainTableView!)
        
        // 设置tableView的数据源
        self.mainTableView!.dataSource = self
        // 设置tableView的委托
        self.mainTableView!.delegate = self
        // 背景颜色
        self.mainTableView.backgroundColor = UIColor.clearColor()
        // 背景图片
        let bgroundView = UIImageView(frame: self.view.bounds)
        bgroundView.image = UIImage(named: "01")
        self.mainTableView.backgroundView = bgroundView
        
        // 其他属性
        self.mainTableView.scrollEnabled = true
        self.mainTableView.scrollsToTop = true
        
        
        // 去掉底端多余分割线,即表尾视图
        let footerLabel = UILabel(frame: CGRectMake(0.0,0.0,CGRectGetWidth(self.mainTableView.frame),100.0))
        footerLabel.backgroundColor = UIColor.greenColor()
        footerLabel.text = "列表视图的表尾视图"
        footerLabel.textAlignment = NSTextAlignment.Center
        self.mainTableView.tableFooterView = footerLabel
        // 表头视图
        let headerLabel = UILabel(frame: CGRectMake(0.0,40.0))
        headerLabel.backgroundColor = UIColor.brownColor()
        headerLabel.text = "列表视图的表头视图"
        headerLabel.textAlignment = NSTextAlignment.Center
        self.mainTableView.tableHeaderView = headerLabel
        
}

// MARK: - 响应事件
func buttonClick(button:UIBarButtonItem) -> Void
{
        let index = button.tag
        if 1 == index
        {
            // 返回顶部
            self.mainTableView.scrollToRowAtIndexPath(NSIndexPath(forRow: 0,inSection: 0),atScrollPosition: UITableViewScrollPosition.Top,animated: true)
        }
        else if 2 == index
        {
            // 回到中间
            self.mainTableView.scrollToRowAtIndexPath(NSIndexPath(forRow: (self.mainArray.count - 1) / 2,atScrollPosition: UITableViewScrollPosition.Middle,animated: true)
        }
        else if 3 == index
        {
            // 去到底部
            self.mainTableView.scrollToRowAtIndexPath(NSIndexPath(forRow: (self.mainArray.count - 1),atScrollPosition: UITableViewScrollPosition.Bottom,animated: true)
        }
}

// MARK: - UITableViewDataSource,UITableViewDelegate
    
func numberOfSectionsInTableView(tableView: UITableView) -> Int
{
        return 1
}
    
func tableView(tableView: UITableView,numberOfRowsInSection section: Int) -> Int
{
        return self.mainArray.count
}
    
func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
        // var cell = tableView.dequeueReusableCellWithIdentifier("UITableViewCell")! // 此写法异常
        var cell:UITableViewCell! = tableView.dequeueReusableCellWithIdentifier("UITableViewCell")
        if (cell == nil)
        {
            cell = UITableViewCell(style: UITableViewCellStyle.Value1,reuseIdentifier: "UITableViewCell")
            
            cell.textLabel!.textColor = UIColor.blackColor()
            cell.textLabel!.text = "当前字体"
            cell.textLabel!.font = UIFont.systemFontOfSize(12.0)
            
            cell.detailTextLabel?.textColor = UIColor.lightGrayColor()
            cell.detailTextLabel!.font = UIFont.systemFontOfSize(12.0)
        }

        // cell附件类型
        cell.accessoryType = UITableViewCellAccessoryType.None
        switch indexPath.row
        {
            case 0:
                cell.accessoryType = UITableViewCellAccessoryType.checkmark
            break
            
            case 1:
                cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
            break
            
            case 2:
                cell.accessoryType = UITableViewCellAccessoryType.DetaildisclosureButton
            break
            
            case 3:
                cell.accessoryType = UITableViewCellAccessoryType.DetailButton
            break
            
            case 4:
                let imageview = UIImageView(image: UIImage(named: "normalImage"))
                imageview.frame = CGRectMake(0.0,30.0,30.0)
                cell.accessoryView = imageview
            break
            
            default:
                cell.accessoryType = UITableViewCellAccessoryType.DetailButton
            break
        }
        
        // cell图标
        cell.imageView!.image = UIImage(named: "normalImage")
        
        let text:String = self.mainArray[indexPath.row as Int] as! String
        cell.detailTextLabel!.text = text
        
        return cell
}
    
func tableView(tableView: UITableView,didSelectRowAtIndexPath indexPath: NSIndexPath)
{
        tableView.deselectRowAtIndexPath(indexPath,animated: true)
}

// MARK: - 附件类型
    
// cell类型为 DetaildisclosureButton,或DetailButton 时的点击响应事件
func tableView(tableView: UITableView,accessoryButtonTappedForRowWithIndexPath indexPath: NSIndexPath) {
        print("点击了:\(indexPath.row)")
}
    
// cell附件类型(注意:不能同时设置cell.accessoryType = UITableViewCellAccessoryType.None)
// func tableView(tableView: UITableView,accessoryTypeForRowWithIndexPath indexPath: NSIndexPath) -> UITableViewCellAccessoryType {
//        return UITableViewCellAccessoryType.checkmark
// }

原文地址:https://www.jb51.cc/swift/322801.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐