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

相关文章

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