ScrollView掛载,为什么会很久才会显示画面

陳尹
  • 348

我的程式如下
其实很简单,就是从网路上跑一个ajax 然后,一个json格式的资料
我有两个func

一个 refreshWithFrame , 一个 indexReFreshFrame

在refreshwithFrame 里我先初始 toolbar ,然后,跑url ,抓到jons 执行 indexfreahframe , 进行json的解析 跟 画面的大小

但是,当我把 mainscrolview 放在 refreshwithframe 掛载时,速度是很快的
但是当我把 mainscrollview被在 indexrefreshfram时,就会非常非常的慢,快二十秒才会显示我的scrollview,可是程式几乎是一秒就跑完了。

可能问题是
1.我网路上抓json的方式错误
2.未知的错误

这个简单的专案我写了好久,都不知道问题出在那里

求大大解 程式如下



class ViewController: UIViewController ,UIToolbarDelegate, UIScrollViewDelegate {

let webImageURL:String = "http://xxxxe.com/upload/"
let webAJAXURL:NSURL = NSURL(string: "http://xxxx.comw/ajax.php")!
var frameWidth:CGFloat = 0.0
var frameHeight:CGFloat = 0.0
var MyFunc:myFunc!
let SPACE:CGFloat = 2.0
var mainScrollView:UIScrollView!
var i_PromoJSON:NSMutableDictionary!
var i_NewsJSON:NSMutableDictionary!
var mainScrollview:UIScrollView!
var toolBar:UIToolbar!


func refreshWithFrame(frame:CGRect){
    self.view.frame = frame
    self.view.backgroundColor = UIColor.clearColor()
    MyFunc = myFunc()
    frameWidth = self.view.frame.size.width
    frameHeight = self.view.frame.size.height
    toolBar = UIToolbar(frame: CGRectMake(0, frameHeight - 40, frameWidth, 40))
    toolBar.barStyle = UIBarStyle.Default
    toolBar.backgroundColor = UIColor.lightGrayColor()
    toolBar.translucent = true
    toolBar.tintColor = UIColor.blackColor()
    toolBar.alpha = 1
    
    let homeBarButton = MyFunc.setBarButton("home.png", eventName: "gotoHome")
    let hearBarButton = MyFunc.setBarButton("heart.png", eventName: "gotoLove")
    let cartBarButton = MyFunc.setBarButton("cart.png", eventName: "gotoCart")
    let memberBarButton = MyFunc.setBarButton("member.png", eventName: "gotoMember")
    
    let barButtonSpace:UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FixedSpace, target: nil, action: nil)
    barButtonSpace.width = (self.view.frame.size.width - (homeBarButton.width * 4 + 20)) / 4
    toolBar.setItems([homeBarButton, barButtonSpace, hearBarButton,barButtonSpace, cartBarButton,barButtonSpace, memberBarButton], animated: true)
    toolBar.userInteractionEnabled = true
    toolBar.sizeToFit()
    toolBar.delegate = self
    self.view.addSubview(toolBar)
    
    //执行网路序
    var postString:String!
    let request = NSMutableURLRequest(URL:webAJAXURL)
    request.HTTPMethod = "POST"
    
    postString = "type=ios&locate=index"
    request.HTTPBody = try! postString.dataUsingEncoding(NSUTF8StringEncoding)
    
    
    
    let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
        data, response, error in
        if error != nil {
            print("\(error)")
            return
        }
        do {
            let myJSON:AnyObject? = try NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments)
            self.indexReFreshFrame(myJSON!)
        }
        catch {print(error)}
    }
    task.resume()
}
    



//MARK: CallBack & Listener
//------------------------------------------------------------

func indexReFreshFrame(myJSON:AnyObject){
    //json解析
    print("得到json")
    //print(myJSON)
    let i_JSON = myJSON as! NSMutableDictionary
    i_PromoJSON = NSMutableDictionary()
    i_NewsJSON = NSMutableDictionary()
    let bannerW:CGFloat = frameWidth
    let bannerH:CGFloat = 100.0
    
    i_PromoJSON.setObject(i_JSON["promo_1"] as! NSMutableArray, forKey: "馆长推荐")
    i_PromoJSON.setObject(i_JSON["promo_2"] as! NSMutableArray, forKey: "新品上市")
    i_PromoJSON.setObject(i_JSON["promo_3"] as! NSMutableArray, forKey: "热销产品")
    i_PromoJSON.setObject(i_JSON["promo_4"] as! NSMutableArray, forKey: "折扣活动")
    i_NewsJSON.setObject(i_JSON["news"] as! NSMutableArray, forKey: "最新消息")
    let i_banner:NSMutableArray = i_JSON["banner"] as! NSMutableArray
    

    //高度计算
    //newstable高度
    var mainScrollViewHeight:CGFloat = 0.0
    var tHeight:Int = 0
    //tHeight = (i_JSON["promo_1"] as! NSMutableArray).count * 180 + 40 + 100
    for var i:Int = 0 ; i < i_PromoJSON.count ; i++ {
        let tarKey:String = i_PromoJSON.allKeys[i] as! String
        var tmpCount:CGFloat = CGFloat((i_PromoJSON.objectForKey(tarKey) as! NSMutableArray).count)
        mainScrollViewHeight += 40
        if tmpCount % 2 == 0 {tmpCount = tmpCount / 2}
        else {tmpCount = ceil(tmpCount / 2) }
        
        mainScrollViewHeight += tmpCount * 200

        
    }
    
    mainScrollViewHeight += 100 + 40 + CGFloat((i_NewsJSON.objectForKey("最新消息") as! NSMutableArray).count * 180) + CGFloat(SPACE * 4)
    print("高度计算")
    mainScrollview = UIScrollView()
    mainScrollview.backgroundColor = UIColor.lightGrayColor()
    mainScrollview.delegate = self
    mainScrollview.showsHorizontalScrollIndicator = false
    mainScrollview.showsVerticalScrollIndicator = true
    //print(toolBar.frame.size.height)
    //mainScrollview.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - toolBar.frame.size.height)
    mainScrollview.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 40)
    mainScrollview.contentSize = CGSizeMake(self.view.frame.size.width, 500)
    self.view.addSubview(mainScrollview)
    print("ScrollView掛载完成")
    //mainScrollViewHeight = CGFloat( (i_JSON["promo_1"] as! NSMutableArray).count * 180 + 40 + SPACE + 100 + SPACE )
    
    
    
}

//Mark   -- Delegate
 //--------------------------------------


}
回复
阅读 1.9k
1 个回答

我试乎发现问题所在了

如果我把画面的部份写在 refreshWithFrame 一切都是正常的
可,当我把addsubview的部份写在其他的func里时,显示都会很慢才出现,这有解吗?

宣传栏