iOS开发之设计模式之代理类

来源:互联网 发布:u8数据库置疑修复工具 编辑:程序博客网 时间:2024/05/21 09:27

首先先说下代理类的概念:

先举个简单的例子:

比如去代售点买火车票,代售点其实并没有火车票,他们也是从火车站里买票然后卖给顾客,他们只是具有买火车票的渠道,这个代售点就是一个代理类

再举个例子:

假如我要删除一条数据,我传个delete给一个类,让它去删除,但是它并没有直接去数据库删除,他是把delete封装成一条语句,又传给了一个类,在这个类中才去操作数据库,封装的这个就是代理类,就是不操作任何东西是链接底层操作和界面的一个类,代理是不操作任何东西与数据的,只是封装一下传递到可以去做这件事情的类中

好了有了上面的两个例子,相信对代理类也有了一定的了解了,现在实现一个具体的demo来更加透彻的分析

现在我要写一个TableView的代理类

贴出重要代码:

代理类:

@objc protocol CardTableViewDelegate:NSObjectProtocol{    // cardView的点击事件    optional func cardTableView(cardTableView: CardTableView, didSelectCardViewAtIndex index: Int)    // 区头的高度    optional func cardTableView(cardTableView: CardTableView, heightForHeaderInSection section: Int) -> CGFloat    // 区尾的高度    optional func cardTableView(cardTableView: CardTableView, heightForFooterInSection section: Int) -> CGFloat    // 自定义区头    optional func cardTableView(cardTableView: CardTableView, viewForHeaderInSection section: Int) -> UIView?    // 自定义区尾    optional func cardTableView(cardTableView: CardTableView, viewForFooterInSection section: Int) -> UIView?    }// 数据源@objc protocol CardTableViewDataSource:NSObjectProtocol{    // cardView的数量    func cardTableView(cardTableView: CardTableView, numberOfCarViewsInSection section: Int) -> Int    // 返回cardView    func cardTableView(cardTableView: CardTableView, cardViewInCardTableView index:Int) -> CardView?    // 返回多少区    optional func numberOfSectionsInCardTableView(cardTableView: CardTableView) -> Int    // cardView区头的标题    optional func cardTableView(cardTableView: CardTableView, titleForHeaderInSection section: Int) -> String?    // cardView区尾的标题    optional func cardTableView(cardTableView: CardTableView, titleForFooterInSection section: Int) -> String?    // cardView的高度    optional func cardTableView(cardTableView: CardTableView, heightForCardViewAtIndex index: Int) -> CGFloat}// 代理类class CardTableView: UIView,UITableViewDelegate,UITableViewDataSource {    // 声明一个私有变量 可以使用UITableView的代理和数据源方法    private var cdTableView:UITableView!        var delegate:CardTableViewDelegate!    var dataSource:CardTableViewDataSource!        // 1 通过设置set get方法可以获得TableView的tableHeaderView等属性    // 2 通过设置代理和数据源代理TableView的代理和数据源    var cardTableHeaderView:UIView!{        get{            return self.cdTableView.tableHeaderView        }        set{            self.cdTableView.tableHeaderView=newValue        }    }    override init(frame: CGRect) {        super.init(frame: frame)        cdTableView = UITableView(frame: frame, style: UITableViewStyle.Plain)        cdTableView.delegate = self        cdTableView.dataSource=self        cdTableView.separatorStyle=UITableViewCellSeparatorStyle.None        cdTableView.registerClass(CardTableViewCell.self, forCellReuseIdentifier: "cell")        self.addSubview(cdTableView)            }        // 下面是代理和数据源方法的调用    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int    {        return self.dataSource.cardTableView(self, numberOfCarViewsInSection: section)    }    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell    {        var cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as CardTableViewCell        // 这里还存在一点小问题,cardView不能直接addSubView,具体如何改善目前还没找到解决方法,望大神指点        var cardView = self.dataSource.cardTableView(self, cardViewInCardTableView: indexPath.row) as CardView?        cell.addSubview(cardView!)                return cell    }    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)    {        tableView.deselectRowAtIndexPath(indexPath, animated: true)        self.delegate.cardTableView!(self, didSelectCardViewAtIndex: indexPath.row)    }    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat    {        return self.dataSource.cardTableView!(self, heightForCardViewAtIndex: indexPath.row)    } 使用代理类:// 声明代理类的数据源和代理class HomePageViewController: BaseNavagationMemberViewController,CardTableViewDelegate,CardTableViewDataSource{    // 代理类    var cardTableView:CardTableView!    override func viewDidLoad() {        super.viewDidLoad()                initWithCardTableView()    }    func initWithCardTableView()    {        cardTableView = CardTableView(frame: CGRectMake(0, 0, KScreenWidth, KScreenHeight))        cardTableView.delegate=self        cardTableView.dataSource=self        cardTableView.backgroundColor = UIColor.greenColor()        self.view.addSubview(cardTableView)                var headerView=UIView(frame: CGRectMake(0, 0, KScreenWidth, 300))        headerView.backgroundColor = UIColor.grayColor()        cardTableView.cardTableHeaderView = headerView                var button = UIButton.buttonWithType(UIButtonType.Custom) as UIButton        button.frame = CGRectMake(60, 60, 120, 40)        button.setTitle("添加/管理卡片", forState: UIControlState.Normal)        button.addTarget(self, action: "buttonClick", forControlEvents: UIControlEvents.TouchUpInside)        headerView.addSubview(button)                    }        // 代理类的数据源和代理    func cardTableView(cardTableView: CardTableView, numberOfCarViewsInSection section: Int) -> Int    {        return 5    }    func cardTableView(cardTableView: CardTableView, cardViewInCardTableView index: Int) -> CardView?    {        var cardView = CardView(frame: CGRectMake(10, 10, KScreenWidth-20, 100))        cardView.backgroundColor = UIColor.redColor()        return cardView    }    func cardTableView(cardTableView: CardTableView, didSelectCardViewAtIndex index: Int)    {        println("开始点击了")    }        func cardTableView(cardTableView: CardTableView, heightForCardViewAtIndex index: Int) -> CGFloat    {                return 120    }效果图:<img src="http://img.blog.csdn.net/20150323215151519?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvSmFtZXNfSm9oblNvbg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />



0 0