swift语言实现提醒、UItableview自动行高、切换视图等

直接上代码


import Foundation
import UIKit

class TableViewController: UIViewController,UITableViewDataSource,UITableViewDelegate{
    var backButton : UIButton!
    var noticeButton : UIButton!
    var personTableView:UITableView!
    var itemAry  = ["a","b","c"]
    var sexAry  = ["1","2","3"]
    var contentAry  = ["桃花坞里桃花庵,桃花庵下桃花仙;桃花仙人种桃树,又摘桃花换酒钱。酒醒只在花前坐,酒醉还来花下眠;半醒半醉日复日,花落花开年复年。但愿老死花酒间,不愿鞠躬车马前;车尘马足富者趣,酒盏花枝贫者缘。别人笑我太疯癫,我笑他人看不穿;不见五陵豪杰墓,无花无酒锄作田。—明·唐伯虎《桃花庵歌》","由注释可以看出,文本框的设置需要获得文字需要占用的尺寸,并且计算文本框占用的大小","311"]
    override func viewDidLoad() {
        println("load TableViewController")
        super.viewDidLoad()
        
        backButton=UIButton() //实例化
        backButton.frame=CGRectMake(20,240,50,30)//大小、位置
        backButton.backgroundColor=UIColor.redColor()//背景色
        backButton.setTitle("Back",forState:UIControlState.Normal)//文本
        backButton.addTarget(self,action:"back2Main:",forControlEvents:UIControlEvents.TouchUpInside)//事件
        
        noticeButton=UIButton() //实例化
        noticeButton.frame=CGRectMake(100,240,50,30)//大小、位置
        noticeButton.backgroundColor=UIColor.redColor()//背景色
        noticeButton.setTitle("Notice",forState:UIControlState.Normal)//文本
        noticeButton.addTarget(self,action:"noticeAction:",forControlEvents:UIControlEvents.TouchUpInside)//事件
        
        
        personTableView=UITableView()
        personTableView.frame=CGRectMake(10, 40, 400, 300)
        personTableView.dataSource=self
        personTableView.delegate=self
        personTableView.registerClass(PersonTableViewCell.self, forCellReuseIdentifier: "cell")//定义新的处理者
        
         NSNotificationCenter.defaultCenter().addObserver(self, selector: "gameOver:", name: "gameOverNotification", object: nil)//定义提醒接收者
        
        self.view.addSubview(backButton);
        self.view.addSubview(noticeButton);
        self.view.addSubview(personTableView);
        
    }
    func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
        
        var cell = tableView.dequeueReusableCellWithIdentifier("cell") as PersonTableViewCell!
       cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
       cell.selectedBackgroundView = nil
        var row=indexPath!.row
        
        cell.reloadCell(self.itemAry[row],sex: self.sexAry[row],content: self.contentAry[row])//加载数据
        return cell
    }
    func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int
    {
        
        return 3
    }
    
    func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath:NSIndexPath) -> CGFloat
    {
       //计算行高,返回,textview根据数据计算高度
        var fixedWidth:CGFloat = 260;
        var contextLab      :   UITextView=UITextView()
        contextLab.text=contentAry[indexPath.row]
        var newSize:CGSize = contextLab.sizeThatFits(CGSizeMake(fixedWidth, 123));
        var height=(newSize.height)
        println("height---\(height)")
        return height+70
    }
    func back2Main(button: UIView) {
        self.view.window?.rootViewController=ViewController()//切换视图
    }
    func noticeAction(button: UIView) {
        NSNotificationCenter.defaultCenter().postNotificationName("gameOverNotification", object: title)//发出提醒
        
    }
    func gameOver(title:NSNotification)
    {
        let alert = UIAlertView()
        alert.title = "计时完成!"
        // var str = title.object as String
        alert.message = "aaa"
        alert.addButtonWithTitle("OK")
        alert.show()

    }
}
class PersonTableViewCell : UITableViewCell{
    
    var IconImage   :   UIImageView?
    var nameLab     :   UILabel?
    var sexLab      :   UILabel?
    var contextLab      :   UITextView?
   
    var bgImage:UIImage?
    required init(coder aDecoder: NSCoder!) {
        super.init(coder: aDecoder)
    }
    override init(style: UITableViewCellStyle, reuseIdentifier: String!){
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        IconImage = UIImageView(frame:CGRectMake(10 , 1 , 60 , 60))
        IconImage!.backgroundColor = UIColor.redColor()
        self.addSubview(IconImage!)
        
        nameLab = UILabel(frame:CGRectMake(70 , 1 , 100 , 20))
        nameLab?.layer.borderWidth=2
        self.addSubview(nameLab!)
        
        sexLab = UILabel(frame:CGRectMake(70 , 21 , 30 , 20))
        sexLab?.layer.borderWidth=1
        self.addSubview(sexLab!)
       
        
        contextLab = UITextView(frame:CGRectMake(10 , 65 , 260 , 50))
           self.addSubview(contextLab!)
        
    }
    
    
    func reloadCell(name:String,sex:String,content:String){
        nameLab!.text = name //NSString
        sexLab!.text = sex //String
        contextLab!.text = content //String
        IconImage?.image=UIImage(named:"mainBack")
        bgImage = UIImage(named:"BlueBubble2")
        
        //计算行高,返回,textview根据数据计算高度变换高度
        var fixedWidth:CGFloat = contextLab!.frame.size.width;
        
        var newSize:CGSize = contextLab!.sizeThatFits(CGSizeMake(fixedWidth, 123));
        println(newSize.height)
        var newFrame:CGRect = contextLab!.frame;
        newFrame.size = CGSizeMake(max(newSize.width, fixedWidth), newSize.height);
        contextLab!.frame = newFrame;

        
    }
    
    
}

你可能感兴趣的:(swift)