Hiding objects within UITableViewCell iOS 8 (Swift)

断了今生、忘了曾经 提交于 2020-01-24 22:35:30

问题


I have a UITableViewCell that on touch, expands (dropdown). The user is then presented with a selection of options. When the user touches one of the options I want to briefly hide all the options, show an activity indicator spinning, display a confirmation message (on success) and then collapse the cell back to normal.

I'm having trouble hiding any object within the custom cell. The following simple code doesn't work (this is the correct superview corresponding to the cell):

var customCell: MyCustomCell = icon.superview.superview.superview as MyCustomCell
customCell.myLabel.hidden = false

I have tried hiding/showing a variety of different objects but nothing works. I've even popped it inside dispatch_async to ensure it runs on the main thread.

Additionally, Xcode 6 beta tells me that myLabel.hidden is read only. This also happens for other objects. Is this no longer the correct way to hide something?

I've had a search around but had no luck in finding an answer. If someone could help me out I'd be grateful! Thanks.


回答1:


The usual way of doing something like this would be to implement the UITableViewDelegate method -tableView:didSelectRowAtIndexPath: to let you know when a row is tapped.

Then you can easily get and modify the cell

var customCell = self.tableView.cellForRowAtIndexPath(indexPath) as MyCustomCell
customCell.myLabel.hidden = false

I consider the line icon.superview.superview.superview to be a code smell. The multiple superview calls makes makes your code fragile and likely to break if you ever change the view hierarchy in your cell.



来源:https://stackoverflow.com/questions/24663294/hiding-objects-within-uitableviewcell-ios-8-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!