2017-05-29 96 views
0

所以我創建了一個iCarousel並能夠成功運行它。然而,我確實需要在視圖控制器上加載多個輪播。我找到了多個iCarousels代碼,並試圖將其適用於Swift。現在不幸的是我得到了一個線程「class AppDelegate」上顯示的Thread1信號SIGABRT。真的不知道爲什麼這樣做。iCarousel - Thread1信號SIGABRT

請幫我一把!下面是我對視圖控制器

class ChangeRoomViewController: UIViewController, iCarouselDataSource, 
iCarouselDelegate { 

    var hatsArray : NSMutableArray = NSMutableArray() 
    var shirtsArray : NSMutableArray = NSMutableArray() 

@IBOutlet weak var carousel1: iCarousel! 
@IBOutlet weak var carousel2: iCarousel! 

override func viewDidLoad() { 

    super.viewDidLoad() 

    hatsArray = ["TopHat.jpeg", "WhiteBrimHat.jpeg", "StoutHat.jpg", "BaseballCapBlackw:Red.jpg", "WhiteBaseballCap.jpg"] 
    carousel1.type = iCarouselType.cylinder 
    carousel1.reloadData() 

    shirtsArray = ["StarWarsBlackFittedT.jpg", "CollaredFittedWhiteWithBeigeSplash.jpg", "CollaredWhiteWithZigzagLightBlueStripes.jpg", "ClooaredBlackWithWhiteStripes.jpg", "CollaredFittedNavyBlue.jpg"] 
    carousel2.type = iCarouselType.cylinder 
    carousel2.reloadData() 
} 

func carousel(_ carousel: iCarousel, viewForItemAt index: Int, reusing view: UIView?) -> UIView { 

    var hatsView : UIImageView! 
    var shirtsView : UIImageView! 

    if view == nil { 
     hatsView = UIImageView(frame: CGRect(x: 16, y: 65, width: 90, height: 60)) 
     hatsView.contentMode = .scaleAspectFit 
     shirtsView = UIImageView(frame: CGRect(x: 16, y: 133, width: 90, height: 60)) 
     shirtsView.contentMode = .scaleAspectFit 
    } else { 
     hatsView = view as! UIImageView 
     shirtsView = view as! UIImageView 
    } 

    hatsView.image = UIImage(named: "\(hatsArray.object(at: index))") 
    shirtsView.image = UIImage(named: "\(shirtsArray.object(at: index))") 

    if (carousel == carousel1) { 
     return hatsView 
    }else { 
     return shirtsView 
    } 

} 

func numberOfItems(in carousel: iCarousel) -> Int { 
    if (carousel == carousel1) { 
     return hatsArray.count 
    }else { 
     return shirtsArray.count 
    } 
} 

}

回答

1

我可以重新創建你的錯誤代碼,它是由shirtsArray造成的是空的,所以線

shirtsView.image = UIImage(named: "\(shirtsArray.object(at: index))") 

使你遇到的異常。

我不知道爲什麼,但如果招行

shirtsArray = ["StarWarsBlackFittedT.jpg", "CollaredFittedWhiteWithBeigeSplash.jpg", "CollaredWhiteWithZigzagLightBlueStripes.jpg", "ClooaredBlackWithWhiteStripes.jpg", "CollaredFittedNavyBlue.jpg"] 

是剛分配到hatsArray下,那麼它的工作原理。

您的viewForItemAt函數有點麻煩。爲什麼在決定哪一個是必需的之前,您是爲這兩個輪播創建圖像/視圖?

+1

非常感謝!這工作!奇怪,雖然它設法排除異常...也許這是由於與「coursel1.reloadData()」衝突「 –

+0

關於你最後一個問題...我想我需要這樣做,因爲兩個輪播將始終加載圖像與此同時。永遠不會有隻有一個傳送帶加載的情況。你能否詳細說明或舉例說明我應該如何處理它? –

+0

該函數將被調用兩次,每個傳送帶一次相應設置傳送帶參數。如果您在函數的開頭測試哪一個,則只需要爲正在調用的特定轉盤設置imageView。 – Nick