Han*_*ghe 0 uicollectionview swift tvos
我正在开发一个应用程序作为appleTV的概念证明.而我正在尝试显示队列.哪个在不同的部门分开.我们有Mac,iPhone,iPad ......我想自动更新我的collectionView以显示哪个部门正在显示.
我已经成功自动更新了我的tableview.
有什么东西看不到吗?
这是我的控制器的代码
class QueueViewController : UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UITableViewDelegate, UITableViewDataSource {
var departments = [Department]()
var users = [User]()
let dateFormatter = NSDateFormatter()
@IBOutlet weak var collectionView: UICollectionView!
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
print("QueueViewController")
collectionView.delegate = self
collectionView.dataSource = self
self.view.backgroundColor = UIColor(patternImage: UIImage(named: "BackgroundQueue")!)
fetchDepartments()
dateFormatter.dateFormat = "HH:mm"
}
override func viewWillAppear(animated: Bool) {
//Hier moet de fetch task worden uitgevoerd
if departments.count > 0 {
startTimer()
}
}
override func viewWillDisappear(animated: Bool) {
stopTimer()
}
//MARK: - CollectionView
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return departments.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! DepartmentItemCell
let department = departments[indexPath.row]
let title = department.name
cell.DepartmentButton.setTitle(title, forState: .Normal)
cell.DepartmentButton.backgroundColor = UIColor.clearColor()
cell.DepartmentButton.restorationIdentifier = department.id
return cell
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
print("indexpath :\(indexPath.row)")
}
@IBAction func selectDepartment(sender: AnyObject) {
fetchQueueForDepartment(sender.restorationIdentifier!!)
}
//MARK: - TableView
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return users.count
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("tableCell") as! QueueItemCell
let user = users[indexPath.row]
if indexPath.row == 0 {
let attributes = [
NSUnderlineStyleAttributeName : 1]
let title = NSAttributedString(string: "\(user.firstname!) \(user.lastname!)", attributes: attributes) //1
cell.nameLabel.attributedText = title
return cell
}
cell.nameLabel.text = "\(user.firstname!) \(user.lastname!)"
cell.arrivalLabel.text = user.arrivalDate == nil ? "" : dateFormatter.stringFromDate(user.arrivalDate!)
return cell
}
//MARK: - Focus update
override func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {
super.didUpdateFocusInContext(context, withAnimationCoordinator: coordinator)
}
//Zorgt ervoor dat je de cellen in collectionview kan selecteren. focus op zetten
func collectionView(collectionView: UICollectionView, canFocusItemAtIndexPath indexPath: NSIndexPath) -> Bool
{
return false
}
//MARK: - Parse
func fetchDepartments(){
SVProgressHUD.show()
ParseService.sharedInstance.fetchDepartments({
(error, data) -> () in
if let json_data = data?["result"] {
self.departments = DepartmentHandler.parseJson(json_data!)
dispatch_async(dispatch_get_main_queue(), {
() -> Void in
print("succeed")
self.collectionView.reloadData()
SVProgressHUD.dismiss()
//Eerste departement
self.fetchQueueForDepartment(self.departments[0].id!)
self.startTimer()
})
}
})
}
func fetchQueueForDepartment(identifier: String){
SVProgressHUD.show()
ParseService.sharedInstance.fetchUsersPerDepartment(identifier,completion:{
(error, data) -> () in
if let json_data = data?["result"] {
self.users = UserHandler.parseJSon(json_data!)
dispatch_async(dispatch_get_main_queue(), {
() -> Void in
print("succeed")
self.tableView.reloadData()
//self.updateFocusCell()
SVProgressHUD.dismiss()
})
}
})
}
//-----------------------------------------------
//For the loop
var timer: dispatch_source_t!
var item = 0
//MARK: - Loop fetch
func startTimer() {
let queue = dispatch_queue_create("com.icapps.caps", nil)
timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue)
dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 10 * NSEC_PER_SEC, 1 * NSEC_PER_SEC) // every 10 seconds, with leeway of 1 second
dispatch_source_set_event_handler(timer) {
// do whatever you want here
let indexPath = NSIndexPath(forRow: self.item, inSection: 0)
self.fetchQueueForDepartment(self.departments[indexPath.row].id!)
self.collectionView.selectItemAtIndexPath(indexPath, animated: true, scrollPosition: .None)
print(self.item)
print(self.departments[indexPath.row].name!)
self.item++
if self.item == self.departments.count {
self.item = 0
}
}
dispatch_resume(timer)
}
func stopTimer() {
if timer != nil {
print("Stop")
dispatch_source_cancel(timer)
timer = nil
}
}
}
Run Code Online (Sandbox Code Playgroud)
您可以使用collectionview Delegate方法,方法名称是
func collectionView(collectionView: UICollectionView, didUpdateFocusInContext context: UICollectionViewFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator)
{
}
Run Code Online (Sandbox Code Playgroud)
您可以像这样使用此方法......
func collectionView(collectionView: UICollectionView, didUpdateFocusInContext context: UICollectionViewFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {
if let previousIndexPath = context.previouslyFocusedIndexPath,
let cell = collectionView.cellForItemAtIndexPath(previousIndexPath) {
cell.contentView.layer.borderWidth = 0.0
cell.contentView.layer.shadowRadius = 0.0
cell.contentView.layer.shadowOpacity = 0
}
if let indexPath = context.nextFocusedIndexPath,
let cell = collectionView.cellForItemAtIndexPath(indexPath) {
cell.contentView.layer.borderWidth = 8.0
cell.contentView.layer.borderColor = UIColor.blackColor().CGColor
cell.contentView.layer.shadowColor = UIColor.blackColor().CGColor
cell.contentView.layer.shadowRadius = 10.0
cell.contentView.layer.shadowOpacity = 0.9
cell.contentView.layer.shadowOffset = CGSize(width: 0, height: 0)
collectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: [.CenteredHorizontally, .CenteredVertically], animated: true)
}
}
Run Code Online (Sandbox Code Playgroud)