gpt4 book ai didi

swift - 突出显示当前 UICollectionViewCell tvOS

转载 作者:行者123 更新时间:2023-11-28 09:34:33 29 4
gpt4 key购买 nike

我目前正在开发一个应用程序作为 appleTV 的概念验证。我正在尝试显示一个队列。在不同的部门是分开的。我们有 Mac、iPhone、iPad……我想自动更新我的 collectionView 以显示哪个部门正在显示。

我已经成功地自动更新了我的 tableview。

有什么东西没看到吗?

enter image description here

这是我的 Controller 的代码

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
}

}
}

最佳答案

可以使用collectionview Delegate方法,方法名是

func collectionView(collectionView: UICollectionView, didUpdateFocusInContext context: UICollectionViewFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) 
{
}

你可以像这样使用这个方法......

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)
}
}

关于swift - 突出显示当前 UICollectionViewCell tvOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34135728/

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