gpt4 book ai didi

ios - 想要使用协议(protocol)构建 Bargraph With Delegate 和 Datasource

转载 作者:行者123 更新时间:2023-11-28 06:31:43 25 4
gpt4 key购买 nike

我正在尝试构建一个条形图 View ,以便为该图 View 创建委托(delegate)和数据源。为图形的委托(delegate)和数据源创建协议(protocol)。

protocol BarGraphDatasource{
func grapgView(grapgView:BarGraph, numberOfItemsInGraph items:Int)->Int
func grapgView(grapgView: BarGraph, didSelectItemAtIndex index: Int)
}

//条形图 View 类如下所示

class BarGraph: UIScrollView {


var delegte: BarGraphDatasource! = nil{

didSet{
self.reloadGraph()
}
}

var barWidth : CGFloat = 24 {

didSet{
self.reloadGraph()
}
}
var minimumSpacing : CGFloat = 10 {

didSet{
self.reloadGraph()
}
}

var numberOfItems : Int = 0 {
didSet{
self.reloadGraph()
}
}
var barColor : UIColor = UIColor.lightGrayColor() {

didSet{
self.reloadGraph()
}
}



init() {
super.init(frame: CGRectZero)
self.reloadGraph()
}

required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
}
func reloadGraph(){
self.setContentSizeForGraph()
}

func setContentSizeForGraph() {

if self.numberOfItems > 0 {
self.contentSize = CGSizeMake((self.barWidth * CGFloat(self.numberOfItems) + self.minimumSpacing), self.frame.height);
var xValue:CGFloat = 0
for i in 1...self.numberOfItems {
xValue = xValue + CGFloat(CGFloat(i) + self.minimumSpacing)
print("xValue: \(xValue)")
let frame = CGRectMake(xValue, 0, self.barWidth, 50)
let barView = UIView(frame: frame)
barView.backgroundColor = self.barColor
self.addSubview(barView)
}
}
}
}

//这是我的 ViewController 类

class ViewController: UIViewController,BarGraphDatasource {

@IBOutlet weak var grapView : BarGraph?

var arrayItems: NSArray = []

override func viewDidLoad() {
super.viewDidLoad()

arrayItems = [1,2,3,4,5]
grapView?.barWidth = 10;
grapView?.delegte = self;

grapView?.reloadGraph()
}


func grapgView(grapgView: BarGraph, numberOfItemsInGraph items: Int) -> Int {
return arrayItems.count // how can i pass this value to the graph view from here *******
}
func grapgView(grapgView: BarGraph, didSelectItemAtIndex index: Int) {
print(arrayItems[index]) // how can i call this method on click of bar view *******
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


}

//这里我无法获取图表,也无法调用 ViewController 类中的协议(protocol)方法

numberOfItemsInGraph 当我们设置这个时,我想将这个值传递给图形中的 BarGraph

didSelectItemAtIndex 如何对选中的项目进行操作

最佳答案

您不需要一直调用 reloadGraph()。当您分配代表时,它将被调用。您可以稍后显式调用它。

第二,您必须从 BarGraph 类的某处调用这些委托(delegate)方法。我已经更改了一些代码,请试试这个。

    import UIKit

protocol BarGraphDatasource{
func grapgView(grapgView:BarGraph, numberOfItemsInGraph items:Int)->Int
func grapgView(grapgView: BarGraph, didSelectItemAtIndex index: Int)
func grapgView(grapgView: BarGraph, setHeightOfBarAtIndex index: Int)->CGFloat
}
// Bargraph View class is like below

class BarGraph: UIScrollView{


var delegte: BarGraphDatasource! = nil{

didSet{
self.reloadGraph()
}
}

var barWidth : CGFloat = 24
var minimumSpacing : CGFloat = 10
var numberOfItems : Int = 0
var barColor : UIColor = UIColor.lightGrayColor()
var barViews : [UIButton] = []


init() {
super.init(frame: CGRectZero)
self.reloadGraph()
}

required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
}
func reloadGraph(){
numberOfItems = delegte!.grapgView(self, numberOfItemsInGraph: numberOfItems)
self.setContentSizeForGraph()

}

func setContentSizeForGraph() {

if self.numberOfItems > 0 {

self.contentSize = CGSizeMake((self.barWidth * CGFloat(self.numberOfItems) + self.minimumSpacing), self.frame.height);
var xValue:CGFloat = 0

for i in 1...self.numberOfItems {
xValue = xValue + CGFloat(CGFloat(i) + self.minimumSpacing + self.barWidth)
print("xValue: \(xValue)")
let frame = CGRectMake(xValue, self.layer.frame.size.height-delegte.grapgView(self, setHeightOfBarAtIndex: i-1), self.barWidth, delegte.grapgView(self, setHeightOfBarAtIndex: i-1))
let barView = UIButton(frame: frame)
barView.backgroundColor = self.barColor
barViews.append(barView)
barView.addTarget(self, action: #selector(BarGraph.send_ButtonIndex(_:)), forControlEvents: UIControlEvents.TouchUpInside)
self.addSubview(barView)
}
}
}

func send_ButtonIndex(barView : UIButton)
{

delegte!.grapgView(self, didSelectItemAtIndex: barViews.indexOf(barView)!)
}

}

这是带有数据源的 BarGraph 类。为了更好地使用,我已经实现了一个额外的委托(delegate)方法。 Viewcontroller 代码如下。

    import UIKit

class ViewController: UIViewController,BarGraphDatasource {

@IBOutlet weak var grapView : BarGraph?

var arrayItems: NSArray = []
var heightOfBars: [CGFloat] = []

override func viewDidLoad() {
super.viewDidLoad()

arrayItems = [1,2,3,4,5]
heightOfBars = [20,30,25,60,40]
grapView?.barWidth = 20;
grapView?.barColor = UIColor.brownColor()
grapView?.minimumSpacing = 15;
grapView?.delegte = self;
}


func grapgView(grapgView: BarGraph, numberOfItemsInGraph items: Int) -> Int {
return arrayItems.count //pass this value to the graph view from here *******
}
func grapgView(grapgView: BarGraph, didSelectItemAtIndex index: Int) {
print(arrayItems[index]) //call this method on click of bar view *******
}

func grapgView(grapgView: BarGraph, setHeightOfBarAtIndex index: Int) -> CGFloat
{
return heightOfBars[index]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


}

关于ios - 想要使用协议(protocol)构建 Bargraph With Delegate 和 Datasource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40131438/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com